File tree Expand file tree Collapse file tree 3 files changed +9
-2
lines changed
Expand file tree Collapse file tree 3 files changed +9
-2
lines changed Original file line number Diff line number Diff line change @@ -285,7 +285,7 @@ Datetimelike
285285
286286Timedelta
287287^^^^^^^^^
288- -
288+ - Bug in :func: ` astype_nansafe ` astype("timedelta64[ns]") fails when np.nan is included ( :issue: ` 45798 `)
289289
290290Time Zones
291291^^^^^^^^^^
Original file line number Diff line number Diff line change 2727 is_datetime64_dtype ,
2828 is_datetime64tz_dtype ,
2929 is_dtype_equal ,
30+ is_integer_dtype ,
3031 is_object_dtype ,
3132 is_timedelta64_dtype ,
3233 pandas_dtype ,
@@ -133,7 +134,7 @@ def astype_nansafe(
133134
134135 raise TypeError (f"cannot astype a timedelta from [{ arr .dtype } ] to [{ dtype } ]" )
135136
136- elif np .issubdtype (arr .dtype , np .floating ) and np . issubdtype (dtype , np . integer ):
137+ elif np .issubdtype (arr .dtype , np .floating ) and is_integer_dtype (dtype ):
137138 return _astype_float_to_int_nansafe (arr , dtype , copy )
138139
139140 elif is_object_dtype (arr .dtype ):
Original file line number Diff line number Diff line change @@ -600,3 +600,9 @@ def test_astype_from_categorical_with_keywords(self):
600600 exp = Series (Categorical (lst , categories = list ("abcdef" ), ordered = True ))
601601 res = ser .astype (CategoricalDtype (list ("abcdef" ), ordered = True ))
602602 tm .assert_series_equal (res , exp )
603+
604+ def test_astype_timedelta64_with_np_nan (self ):
605+ # GH45798
606+ result = Series ([Timedelta (1 ), np .nan ], dtype = "timedelta64[ns]" )
607+ expected = Series ([Timedelta (1 ), NaT ], dtype = "timedelta64[ns]" )
608+ tm .assert_series_equal (result , expected )
You can’t perform that action at this time.
0 commit comments