Skip to content

BUG: astype with timedelta and datetime string (#22100) #22107

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

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion pandas/core/dtypes/cast.py
Original file line number Diff line number Diff line change
Expand Up @@ -712,7 +712,8 @@ def astype_nansafe(arr, dtype, copy=True):
elif is_object_dtype(arr):

# work around NumPy brokenness, #1987
if np.issubdtype(dtype.type, np.integer):
is_time = is_datetime64_dtype(dtype) or is_timedelta64_dtype(dtype)
if np.issubdtype(dtype.type, np.integer) and not is_time:
return lib.astype_intsafe(arr.ravel(), dtype).reshape(arr.shape)

# if we have a datetime/timedelta array of objects
Expand Down
15 changes: 14 additions & 1 deletion pandas/tests/dtypes/test_cast.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
find_common_type,
construct_1d_object_array_from_listlike,
construct_1d_ndarray_preserving_na,
construct_1d_arraylike_from_scalar)
construct_1d_arraylike_from_scalar,
astype_nansafe)
from pandas.core.dtypes.dtypes import (
CategoricalDtype,
DatetimeTZDtype,
Expand Down Expand Up @@ -456,3 +457,15 @@ def test_cast_1d_arraylike_from_scalar_categorical(self):
def test_construct_1d_ndarray_preserving_na(values, dtype, expected):
result = construct_1d_ndarray_preserving_na(values, dtype=dtype)
tm.assert_numpy_array_equal(result, expected)


@pytest.mark.parametrize('arr, dtype', [
(np.array(['0:0:1'], dtype='object'), 'timedelta64[ns]'),
(np.array(['0:0:1'], dtype='object'), 'timedelta64'),
(np.array(['2000'], dtype='object'), 'datetime64[ns]'),
(np.array(['2000'], dtype='object'), 'datetime64'),
])
def test_astype_nansafe(arr, dtype):
# GH #22100
result = astype_nansafe(arr, dtype)
is_dtype_equal(result.dtype, dtype)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should there be an assertion here?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Piggybacking on this I think it would be better to explicitly parametrize the expected value and make assertions around that.

On a somewhat related note how do we want to handle datetime64[ns, tz]? Off the top of my head I can't think of how to do a conversion to that via astype so maybe should still raise for that?