Skip to content

BUG: Fix all-NaT when ArrowEA.astype to categorical #62055

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

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 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
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v3.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -693,7 +693,7 @@ Categorical
- Bug in :meth:`Categorical.astype` where ``copy=False`` would still trigger a copy of the codes (:issue:`62000`)
- Bug in :meth:`DataFrame.pivot` and :meth:`DataFrame.set_index` raising an ``ArrowNotImplementedError`` for columns with pyarrow dictionary dtype (:issue:`53051`)
- Bug in :meth:`Series.convert_dtypes` with ``dtype_backend="pyarrow"`` where empty :class:`CategoricalDtype` :class:`Series` raised an error or got converted to ``null[pyarrow]`` (:issue:`59934`)
-
- Bug in :meth:`array.astype` where casting a pyarrow-backed array to a temporal :class:`CategoricalDtype` (e.g. with datetime or timedelta categories) raised or incorrectly converted values to all ``NaT`` (:issue:`62051`)

Datetimelike
^^^^^^^^^^^^
Expand Down
7 changes: 7 additions & 0 deletions pandas/core/arrays/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -757,13 +757,20 @@ def astype(self, dtype: AstypeArg, copy: bool = True) -> ArrayLike:
>>> arr2.dtype
dtype('float64')
"""
from pandas.api.types import CategoricalDtype

dtype = pandas_dtype(dtype)
if dtype == self.dtype:
if not copy:
return self
else:
return self.copy()

if isinstance(dtype, CategoricalDtype):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pretty weird to do this in the base class. Can you track down where the actual problem is? Could be hiding other bugs eg in categorical.fromsequence

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah okay i'll investigate further

from pandas.core.arrays import Categorical

return Categorical(self.to_numpy(), dtype=dtype)

if isinstance(dtype, ExtensionDtype):
cls = dtype.construct_array_type()
return cls._from_sequence(self, dtype=dtype, copy=copy)
Expand Down
19 changes: 19 additions & 0 deletions pandas/tests/arrays/categorical/test_astype.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,17 @@
CategoricalDtype,
CategoricalIndex,
DatetimeIndex,
Index,
Interval,
NaT,
Period,
Timestamp,
array,
isna,
to_datetime,
)
import pandas._testing as tm
from pandas.core.arrays.arrow.array import ArrowExtensionArray


class TestAstype:
Expand Down Expand Up @@ -160,3 +163,19 @@ def test_astype_category_readonly_mask_values(self):
result = arr.astype("category")
expected = array([0, 1, 2], dtype="Int64").astype("category")
tm.assert_extension_array_equal(result, expected)

def test_arrow_array_astype_to_categorical_dtype_temporal(self):
# GH#62051
arr = array(
["2017-01-01", "2018-01-01", "2019-01-01"], dtype="date32[day][pyarrow]"
)
cats = Index(["2017-01-01", "2018-01-01", "2019-01-01"], dtype="M8[s]")
dtype = CategoricalDtype(categories=cats, ordered=False)

assert not all(isna(arr.astype(dtype)))

arr = ArrowExtensionArray._from_sequence(["1h", "2h", "3h"])
cats = Index(["1h", "2h", "3h"], dtype="m8[ns]")
dtype = CategoricalDtype(cats, ordered=False)

assert not all(isna(arr.astype(dtype)))
Loading