From 8758f39c126adc6d1aad3e94f29f5c9c57cafd58 Mon Sep 17 00:00:00 2001 From: cheanwei Date: Sun, 13 Feb 2022 00:19:28 +0800 Subject: [PATCH 1/7] astype timedelta64[ns] fails when np.nan is included --- pandas/core/dtypes/astype.py | 3 ++- pandas/tests/series/indexing/test_datetime.py | 6 ++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/pandas/core/dtypes/astype.py b/pandas/core/dtypes/astype.py index 1e78bf0cd33ae..beb82fd47964b 100644 --- a/pandas/core/dtypes/astype.py +++ b/pandas/core/dtypes/astype.py @@ -134,7 +134,8 @@ def astype_nansafe( raise TypeError(f"cannot astype a timedelta from [{arr.dtype}] to [{dtype}]") elif np.issubdtype(arr.dtype, np.floating) and np.issubdtype(dtype, np.integer): - return _astype_float_to_int_nansafe(arr, dtype, copy) + if dtype.kind != "m": + return _astype_float_to_int_nansafe(arr, dtype, copy) elif is_object_dtype(arr.dtype): diff --git a/pandas/tests/series/indexing/test_datetime.py b/pandas/tests/series/indexing/test_datetime.py index b8291471225d7..62504003bbcef 100644 --- a/pandas/tests/series/indexing/test_datetime.py +++ b/pandas/tests/series/indexing/test_datetime.py @@ -482,3 +482,9 @@ def test_getitem_str_second_with_datetimeindex(): msg = r"Timestamp\('2012-01-02 18:01:02-0600', tz='US/Central', freq='S'\)" with pytest.raises(KeyError, match=msg): df[df.index[2]] + + +def test_astype_timedelta64_with_np_nan(): + result = Series([1, np.nan]).astype("timedelta64[ns]") + expected = Series([1, pd.NaT]).astype("timedelta64[ns]") + tm.assert_series_equal(result, expected) From 2af174c01a0370722c017b7bc19cc0a493355606 Mon Sep 17 00:00:00 2001 From: cheanwei Date: Sun, 13 Feb 2022 00:22:17 +0800 Subject: [PATCH 2/7] add github issue number --- pandas/tests/series/indexing/test_datetime.py | 1 + 1 file changed, 1 insertion(+) diff --git a/pandas/tests/series/indexing/test_datetime.py b/pandas/tests/series/indexing/test_datetime.py index 62504003bbcef..e23cbe956b437 100644 --- a/pandas/tests/series/indexing/test_datetime.py +++ b/pandas/tests/series/indexing/test_datetime.py @@ -485,6 +485,7 @@ def test_getitem_str_second_with_datetimeindex(): def test_astype_timedelta64_with_np_nan(): + # GH45798 result = Series([1, np.nan]).astype("timedelta64[ns]") expected = Series([1, pd.NaT]).astype("timedelta64[ns]") tm.assert_series_equal(result, expected) From 55e63f5a4301e3a4333e5a5007066ea44731b097 Mon Sep 17 00:00:00 2001 From: "chean.wei.khor" Date: Wed, 16 Feb 2022 20:33:54 +0800 Subject: [PATCH 3/7] update np.issubdtype to is_integer_dtype --- pandas/core/dtypes/astype.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pandas/core/dtypes/astype.py b/pandas/core/dtypes/astype.py index beb82fd47964b..daae491b09c8a 100644 --- a/pandas/core/dtypes/astype.py +++ b/pandas/core/dtypes/astype.py @@ -27,6 +27,7 @@ is_datetime64_dtype, is_datetime64tz_dtype, is_dtype_equal, + is_integer_dtype, is_object_dtype, is_timedelta64_dtype, pandas_dtype, @@ -133,9 +134,8 @@ def astype_nansafe( raise TypeError(f"cannot astype a timedelta from [{arr.dtype}] to [{dtype}]") - elif np.issubdtype(arr.dtype, np.floating) and np.issubdtype(dtype, np.integer): - if dtype.kind != "m": - return _astype_float_to_int_nansafe(arr, dtype, copy) + elif np.issubdtype(arr.dtype, np.floating) and is_integer_dtype(dtype): + return _astype_float_to_int_nansafe(arr, dtype, copy) elif is_object_dtype(arr.dtype): From 3c7ce1f5a55bad9e18e4c848871a231d522afe60 Mon Sep 17 00:00:00 2001 From: "chean.wei.khor" Date: Thu, 17 Feb 2022 22:09:40 +0800 Subject: [PATCH 4/7] change test case to another file --- pandas/tests/series/indexing/test_datetime.py | 7 ------- pandas/tests/series/methods/test_astype.py | 6 ++++++ 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/pandas/tests/series/indexing/test_datetime.py b/pandas/tests/series/indexing/test_datetime.py index e23cbe956b437..b8291471225d7 100644 --- a/pandas/tests/series/indexing/test_datetime.py +++ b/pandas/tests/series/indexing/test_datetime.py @@ -482,10 +482,3 @@ def test_getitem_str_second_with_datetimeindex(): msg = r"Timestamp\('2012-01-02 18:01:02-0600', tz='US/Central', freq='S'\)" with pytest.raises(KeyError, match=msg): df[df.index[2]] - - -def test_astype_timedelta64_with_np_nan(): - # GH45798 - result = Series([1, np.nan]).astype("timedelta64[ns]") - expected = Series([1, pd.NaT]).astype("timedelta64[ns]") - tm.assert_series_equal(result, expected) diff --git a/pandas/tests/series/methods/test_astype.py b/pandas/tests/series/methods/test_astype.py index ff9bb3edeedb1..6bda23368411a 100644 --- a/pandas/tests/series/methods/test_astype.py +++ b/pandas/tests/series/methods/test_astype.py @@ -595,3 +595,9 @@ def test_astype_from_categorical_with_keywords(self): exp = Series(Categorical(lst, categories=list("abcdef"), ordered=True)) res = ser.astype(CategoricalDtype(list("abcdef"), ordered=True)) tm.assert_series_equal(res, exp) + + def test_astype_timedelta64_with_np_nan(self): + # GH45798 + result = Series([1, np.nan]).astype("timedelta64[ns]") + expected = Series([1, NaT]).astype("timedelta64[ns]") + tm.assert_series_equal(result, expected) From 90509274ce0d0c44acd4085b063908d1e94cd911 Mon Sep 17 00:00:00 2001 From: "chean.wei.khor" Date: Mon, 21 Feb 2022 19:41:35 +0800 Subject: [PATCH 5/7] change series command --- pandas/tests/series/methods/test_astype.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pandas/tests/series/methods/test_astype.py b/pandas/tests/series/methods/test_astype.py index 6bda23368411a..c187517e6b799 100644 --- a/pandas/tests/series/methods/test_astype.py +++ b/pandas/tests/series/methods/test_astype.py @@ -598,6 +598,6 @@ def test_astype_from_categorical_with_keywords(self): def test_astype_timedelta64_with_np_nan(self): # GH45798 - result = Series([1, np.nan]).astype("timedelta64[ns]") - expected = Series([1, NaT]).astype("timedelta64[ns]") + result = Series([Timedelta(1), np.nan], dtype="timedelta64[ns]") + expected = Series([Timedelta(1), NaT], dtype="timedelta64[ns]") tm.assert_series_equal(result, expected) From 16c46ef6b7c73c2a9259c86c60367648000c367d Mon Sep 17 00:00:00 2001 From: Khor Chean Wei Date: Sun, 27 Feb 2022 14:09:50 +0800 Subject: [PATCH 6/7] Update v1.5.0.rst --- doc/source/whatsnew/v1.5.0.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v1.5.0.rst b/doc/source/whatsnew/v1.5.0.rst index 7e470a51858ce..0f44995d55af5 100644 --- a/doc/source/whatsnew/v1.5.0.rst +++ b/doc/source/whatsnew/v1.5.0.rst @@ -284,7 +284,7 @@ Datetimelike Timedelta ^^^^^^^^^ -- +- Bug in :meth:`astype_nansafe` astype("timedelta64[ns]") fails when np.nan is included (:issue:`45798`) Time Zones ^^^^^^^^^^ From 0a99806521d5bd72216f5a5f0daeb5f7f54d7b9c Mon Sep 17 00:00:00 2001 From: Khor Chean Wei Date: Sun, 27 Feb 2022 14:10:49 +0800 Subject: [PATCH 7/7] Update v1.5.0.rst --- doc/source/whatsnew/v1.5.0.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v1.5.0.rst b/doc/source/whatsnew/v1.5.0.rst index 0f44995d55af5..a4bf1e484754a 100644 --- a/doc/source/whatsnew/v1.5.0.rst +++ b/doc/source/whatsnew/v1.5.0.rst @@ -284,7 +284,7 @@ Datetimelike Timedelta ^^^^^^^^^ -- Bug in :meth:`astype_nansafe` astype("timedelta64[ns]") fails when np.nan is included (:issue:`45798`) +- Bug in :func:`astype_nansafe` astype("timedelta64[ns]") fails when np.nan is included (:issue:`45798`) Time Zones ^^^^^^^^^^