Skip to content

BUG: stringdtype.astype(dt64_or_td64) #50734

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

Merged
merged 3 commits into from
Jan 17, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v2.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -946,6 +946,7 @@ Conversion
Strings
^^^^^^^
- Bug in :func:`pandas.api.dtypes.is_string_dtype` that would not return ``True`` for :class:`StringDtype` (:issue:`15585`)
- Bug in converting string dtypes to "datetime64[ns]" or "timedelta64[ns]" incorrectly raising ``TypeError`` (:issue:`36153`)
-

Interval
Expand Down
12 changes: 12 additions & 0 deletions pandas/core/arrays/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,11 @@

from pandas.core.dtypes.cast import maybe_cast_to_extension_array
from pandas.core.dtypes.common import (
is_datetime64_dtype,
is_dtype_equal,
is_list_like,
is_scalar,
is_timedelta64_dtype,
pandas_dtype,
)
from pandas.core.dtypes.dtypes import ExtensionDtype
Expand Down Expand Up @@ -580,6 +582,16 @@ def astype(self, dtype: AstypeArg, copy: bool = True) -> ArrayLike:
cls = dtype.construct_array_type()
return cls._from_sequence(self, dtype=dtype, copy=copy)

elif is_datetime64_dtype(dtype):
from pandas.core.arrays import DatetimeArray

return DatetimeArray._from_sequence(self, dtype=dtype, copy=copy)

elif is_timedelta64_dtype(dtype):
from pandas.core.arrays import TimedeltaArray

return TimedeltaArray._from_sequence(self, dtype=dtype, copy=copy)

return np.array(self, dtype=dtype, copy=copy)

def isna(self) -> np.ndarray | ExtensionArraySupportsAnyAll:
Expand Down
12 changes: 1 addition & 11 deletions pandas/tests/arrays/string_/test_string.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,17 +73,7 @@ def test_setitem_with_scalar_string(dtype):
tm.assert_extension_array_equal(arr, expected)


def test_astype_roundtrip(dtype, request):
if dtype.storage == "pyarrow":
reason = "ValueError: Could not convert object to NumPy datetime"
mark = pytest.mark.xfail(reason=reason, raises=ValueError)
request.node.add_marker(mark)
else:
mark = pytest.mark.xfail(
reason="GH#36153 casting from StringArray to dt64 fails", raises=ValueError
)
request.node.add_marker(mark)

def test_astype_roundtrip(dtype):
ser = pd.Series(pd.date_range("2000", periods=12))
ser[0] = None

Expand Down
4 changes: 1 addition & 3 deletions pandas/tests/series/methods/test_astype.py
Original file line number Diff line number Diff line change
Expand Up @@ -454,9 +454,7 @@ class TestAstypeString:
def test_astype_string_to_extension_dtype_roundtrip(
self, data, dtype, request, nullable_string_dtype
):
if dtype == "boolean" or (
dtype in ("datetime64[ns]", "timedelta64[ns]") and NaT in data
):
if dtype == "boolean" or (dtype == "timedelta64[ns]" and NaT in data):
mark = pytest.mark.xfail(
reason="TODO StringArray.astype() with missing values #GH40566"
)
Expand Down