Skip to content

Commit 0e0a447

Browse files
authored
BUG: stringdtype.astype(dt64_or_td64) (#50734)
* BUG: stringdtype.astype(dt64_or_td64) * fix test
1 parent 7f8baa0 commit 0e0a447

File tree

4 files changed

+15
-14
lines changed

4 files changed

+15
-14
lines changed

doc/source/whatsnew/v2.0.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -954,6 +954,7 @@ Conversion
954954
Strings
955955
^^^^^^^
956956
- Bug in :func:`pandas.api.dtypes.is_string_dtype` that would not return ``True`` for :class:`StringDtype` (:issue:`15585`)
957+
- Bug in converting string dtypes to "datetime64[ns]" or "timedelta64[ns]" incorrectly raising ``TypeError`` (:issue:`36153`)
957958
-
958959

959960
Interval

pandas/core/arrays/base.py

+12
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,11 @@
5555

5656
from pandas.core.dtypes.cast import maybe_cast_to_extension_array
5757
from pandas.core.dtypes.common import (
58+
is_datetime64_dtype,
5859
is_dtype_equal,
5960
is_list_like,
6061
is_scalar,
62+
is_timedelta64_dtype,
6163
pandas_dtype,
6264
)
6365
from pandas.core.dtypes.dtypes import ExtensionDtype
@@ -580,6 +582,16 @@ def astype(self, dtype: AstypeArg, copy: bool = True) -> ArrayLike:
580582
cls = dtype.construct_array_type()
581583
return cls._from_sequence(self, dtype=dtype, copy=copy)
582584

585+
elif is_datetime64_dtype(dtype):
586+
from pandas.core.arrays import DatetimeArray
587+
588+
return DatetimeArray._from_sequence(self, dtype=dtype, copy=copy)
589+
590+
elif is_timedelta64_dtype(dtype):
591+
from pandas.core.arrays import TimedeltaArray
592+
593+
return TimedeltaArray._from_sequence(self, dtype=dtype, copy=copy)
594+
583595
return np.array(self, dtype=dtype, copy=copy)
584596

585597
def isna(self) -> np.ndarray | ExtensionArraySupportsAnyAll:

pandas/tests/arrays/string_/test_string.py

+1-11
Original file line numberDiff line numberDiff line change
@@ -73,17 +73,7 @@ def test_setitem_with_scalar_string(dtype):
7373
tm.assert_extension_array_equal(arr, expected)
7474

7575

76-
def test_astype_roundtrip(dtype, request):
77-
if dtype.storage == "pyarrow":
78-
reason = "ValueError: Could not convert object to NumPy datetime"
79-
mark = pytest.mark.xfail(reason=reason, raises=ValueError)
80-
request.node.add_marker(mark)
81-
else:
82-
mark = pytest.mark.xfail(
83-
reason="GH#36153 casting from StringArray to dt64 fails", raises=ValueError
84-
)
85-
request.node.add_marker(mark)
86-
76+
def test_astype_roundtrip(dtype):
8777
ser = pd.Series(pd.date_range("2000", periods=12))
8878
ser[0] = None
8979

pandas/tests/series/methods/test_astype.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -454,9 +454,7 @@ class TestAstypeString:
454454
def test_astype_string_to_extension_dtype_roundtrip(
455455
self, data, dtype, request, nullable_string_dtype
456456
):
457-
if dtype == "boolean" or (
458-
dtype in ("datetime64[ns]", "timedelta64[ns]") and NaT in data
459-
):
457+
if dtype == "boolean" or (dtype == "timedelta64[ns]" and NaT in data):
460458
mark = pytest.mark.xfail(
461459
reason="TODO StringArray.astype() with missing values #GH40566"
462460
)

0 commit comments

Comments
 (0)