Skip to content

Commit 8cb0654

Browse files
committed
BUG: astype with timedelta and datetime string (pandas-dev#22100)
1 parent 114f415 commit 8cb0654

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

pandas/core/dtypes/cast.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -712,7 +712,7 @@ def astype_nansafe(arr, dtype, copy=True):
712712
elif is_object_dtype(arr):
713713

714714
# work around NumPy brokenness, #1987
715-
if np.issubdtype(dtype.type, np.integer):
715+
if np.issubdtype(dtype.type, np.integer) and not is_datetime64_dtype(dtype) and not is_timedelta64_dtype(dtype):
716716
return lib.astype_intsafe(arr.ravel(), dtype).reshape(arr.shape)
717717

718718
# if we have a datetime/timedelta array of objects

pandas/tests/dtypes/test_cast.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@
2424
find_common_type,
2525
construct_1d_object_array_from_listlike,
2626
construct_1d_ndarray_preserving_na,
27-
construct_1d_arraylike_from_scalar)
27+
construct_1d_arraylike_from_scalar,
28+
astype_nansafe)
2829
from pandas.core.dtypes.dtypes import (
2930
CategoricalDtype,
3031
DatetimeTZDtype,
@@ -456,3 +457,14 @@ def test_cast_1d_arraylike_from_scalar_categorical(self):
456457
def test_construct_1d_ndarray_preserving_na(values, dtype, expected):
457458
result = construct_1d_ndarray_preserving_na(values, dtype=dtype)
458459
tm.assert_numpy_array_equal(result, expected)
460+
461+
@pytest.mark.parametrize('arr, dtype, expected', [
462+
(np.array(['0:0:1'], dtype='object'), 'timedelta64[ns]', 'timedelta64[ns]'),
463+
(np.array(['0:0:1'], dtype='object'), 'timedelta64', 'timedelta64'),
464+
(np.array(['2000'], dtype='object'), 'datetime64[ns]', 'datetime64[ns]'),
465+
(np.array(['2000'], dtype='object'), 'datetime64', 'datetime64'),
466+
])
467+
def test_astype_nansafe(arr, dtype, expected):
468+
# GH #22100
469+
result = astype_nansafe(arr, dtype)
470+
is_dtype_equal(arr.dtype, expected)

0 commit comments

Comments
 (0)