File tree 3 files changed +9
-2
lines changed
3 files changed +9
-2
lines changed Original file line number Diff line number Diff line change @@ -285,7 +285,7 @@ Datetimelike
285
285
286
286
Timedelta
287
287
^^^^^^^^^
288
- -
288
+ - Bug in :func: ` astype_nansafe ` astype("timedelta64[ns]") fails when np.nan is included ( :issue: ` 45798 `)
289
289
290
290
Time Zones
291
291
^^^^^^^^^^
Original file line number Diff line number Diff line change 27
27
is_datetime64_dtype ,
28
28
is_datetime64tz_dtype ,
29
29
is_dtype_equal ,
30
+ is_integer_dtype ,
30
31
is_object_dtype ,
31
32
is_timedelta64_dtype ,
32
33
pandas_dtype ,
@@ -133,7 +134,7 @@ def astype_nansafe(
133
134
134
135
raise TypeError (f"cannot astype a timedelta from [{ arr .dtype } ] to [{ dtype } ]" )
135
136
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 ):
137
138
return _astype_float_to_int_nansafe (arr , dtype , copy )
138
139
139
140
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):
600
600
exp = Series (Categorical (lst , categories = list ("abcdef" ), ordered = True ))
601
601
res = ser .astype (CategoricalDtype (list ("abcdef" ), ordered = True ))
602
602
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