From 6f8470d1780939566e91f32a7929c03f1b75c81d Mon Sep 17 00:00:00 2001 From: Luke Manley Date: Sat, 19 Nov 2022 21:58:34 -0500 Subject: [PATCH 1/2] check for np.dtype --- pandas/core/arrays/timedeltas.py | 2 +- pandas/tests/extension/test_arrow.py | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/pandas/core/arrays/timedeltas.py b/pandas/core/arrays/timedeltas.py index fe7cade1711d0..4f9f1820923da 100644 --- a/pandas/core/arrays/timedeltas.py +++ b/pandas/core/arrays/timedeltas.py @@ -317,7 +317,7 @@ def astype(self, dtype, copy: bool = True): # DatetimeLikeArrayMixin super call handles other cases dtype = pandas_dtype(dtype) - if dtype.kind == "m": + if isinstance(dtype, np.dtype) and dtype.kind == "m": if dtype == self.dtype: if copy: return self.copy() diff --git a/pandas/tests/extension/test_arrow.py b/pandas/tests/extension/test_arrow.py index d094a7731c417..25cd5f4669746 100644 --- a/pandas/tests/extension/test_arrow.py +++ b/pandas/tests/extension/test_arrow.py @@ -1369,3 +1369,12 @@ def test_pickle_roundtrip(data): result_sliced = pickle.loads(sliced_pickled) tm.assert_series_equal(result_sliced, expected_sliced) + + +def test_astype_from_non_pyarrow(data): + # GH# + pd_array = data._data.to_pandas().array + result = pd_array.astype(data.dtype) + assert not isinstance(pd_array.dtype, ArrowDtype) + assert isinstance(result.dtype, ArrowDtype) + tm.assert_extension_array_equal(result, data) From 580a3e383be8431258f5003cdf20def7a63307d3 Mon Sep 17 00:00:00 2001 From: Luke Manley Date: Sat, 19 Nov 2022 22:18:26 -0500 Subject: [PATCH 2/2] whatsnew --- doc/source/whatsnew/v2.0.0.rst | 2 ++ pandas/tests/extension/test_arrow.py | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v2.0.0.rst b/doc/source/whatsnew/v2.0.0.rst index 0e6d1029d352b..8c3b8cc573096 100644 --- a/doc/source/whatsnew/v2.0.0.rst +++ b/doc/source/whatsnew/v2.0.0.rst @@ -652,6 +652,8 @@ Conversion - Bug where any :class:`ExtensionDtype` subclass with ``kind="M"`` would be interpreted as a timezone type (:issue:`34986`) - Bug in :class:`.arrays.ArrowExtensionArray` that would raise ``NotImplementedError`` when passed a sequence of strings or binary (:issue:`49172`) - Bug in :func:`to_datetime` was not respecting ``exact`` argument when ``format`` was an ISO8601 format (:issue:`12649`) +- Bug in :meth:`TimedeltaArray.astype` raising ``TypeError`` when converting to a pyarrow duration type (:issue:`49795`) +- Strings ^^^^^^^ diff --git a/pandas/tests/extension/test_arrow.py b/pandas/tests/extension/test_arrow.py index 25cd5f4669746..4e1ba89170711 100644 --- a/pandas/tests/extension/test_arrow.py +++ b/pandas/tests/extension/test_arrow.py @@ -1372,7 +1372,7 @@ def test_pickle_roundtrip(data): def test_astype_from_non_pyarrow(data): - # GH# + # GH49795 pd_array = data._data.to_pandas().array result = pd_array.astype(data.dtype) assert not isinstance(pd_array.dtype, ArrowDtype)