BUG: stringdtype.astype(dt64_or_td64) (#50734) · pandas-dev/pandas@0e0a447 (original) (raw)
4 files changed
lines changed
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -954,6 +954,7 @@ Conversion | ||
954 | 954 | Strings |
955 | 955 | ^^^^^^^ |
956 | 956 | - 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`) | |
957 | 958 | - |
958 | 959 | |
959 | 960 | Interval |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -55,9 +55,11 @@ | ||
55 | 55 | |
56 | 56 | from pandas.core.dtypes.cast import maybe_cast_to_extension_array |
57 | 57 | from pandas.core.dtypes.common import ( |
58 | +is_datetime64_dtype, | |
58 | 59 | is_dtype_equal, |
59 | 60 | is_list_like, |
60 | 61 | is_scalar, |
62 | +is_timedelta64_dtype, | |
61 | 63 | pandas_dtype, |
62 | 64 | ) |
63 | 65 | from pandas.core.dtypes.dtypes import ExtensionDtype |
@@ -580,6 +582,16 @@ def astype(self, dtype: AstypeArg, copy: bool = True) -> ArrayLike: | ||
580 | 582 | cls = dtype.construct_array_type() |
581 | 583 | return cls._from_sequence(self, dtype=dtype, copy=copy) |
582 | 584 | |
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 | + | |
583 | 595 | return np.array(self, dtype=dtype, copy=copy) |
584 | 596 | |
585 | 597 | def isna(self) -> np.ndarray | ExtensionArraySupportsAnyAll: |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -73,17 +73,7 @@ def test_setitem_with_scalar_string(dtype): | ||
73 | 73 | tm.assert_extension_array_equal(arr, expected) |
74 | 74 | |
75 | 75 | |
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): | |
87 | 77 | ser = pd.Series(pd.date_range("2000", periods=12)) |
88 | 78 | ser[0] = None |
89 | 79 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -454,9 +454,7 @@ class TestAstypeString: | ||
454 | 454 | def test_astype_string_to_extension_dtype_roundtrip( |
455 | 455 | self, data, dtype, request, nullable_string_dtype |
456 | 456 | ): |
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): | |
460 | 458 | mark = pytest.mark.xfail( |
461 | 459 | reason="TODO StringArray.astype() with missing values #GH40566" |
462 | 460 | ) |