-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
Bug fix - Astype Timedelta64[ns] fails when np.nan is included #45965
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Bug fix - Astype Timedelta64[ns] fails when np.nan is included #45965
Conversation
pandas/core/dtypes/astype.py
Outdated
@@ -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): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i think use is_integer_dtype(dtype)
instead of issubdtype would be simpler
@@ -482,3 +482,10 @@ 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]] | |||
|
|||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
these are astype tests, pls move them.
def test_astype_timedelta64_with_np_nan(): | ||
# GH45798 | ||
result = Series([1, np.nan]).astype("timedelta64[ns]") | ||
expected = Series([1, pd.NaT]).astype("timedelta64[ns]") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
to be explictily construct here the expected
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi I do not understand. Can please give more details? Thank.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if the method being tested is Series.astype, then the test belongs in tests.series.methods.test_astype
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
great, can you add a whatsnew note in 1.5 bug fixes in the timedelta section (or dtype conversion ok too).
pls merge master and ping on green.
…timedelta64_with_np_nan
@jreback @jbrockmendel |
thanks @weikhor |
@@ -284,7 +284,7 @@ Datetimelike | |||
|
|||
Timedelta | |||
^^^^^^^^^ | |||
- | |||
- Bug in :func:`astype_nansafe` astype("timedelta64[ns]") fails when np.nan is included (:issue:`45798`) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is there a user-facing method we can refer to?
astype("timedelta64[ns]")
fails whennp.nan
is included #45798doc/source/whatsnew/vX.X.X.rst
file if fixing a bug or adding a new feature.