Skip to content

Commit a5fab10

Browse files
committed
Updated fix logic
1 parent 5987952 commit a5fab10

File tree

2 files changed

+13
-13
lines changed

2 files changed

+13
-13
lines changed

pandas/core/arrays/base.py

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -757,26 +757,13 @@ def astype(self, dtype: AstypeArg, copy: bool = True) -> ArrayLike:
757757
>>> arr2.dtype
758758
dtype('float64')
759759
"""
760-
from pandas.api.types import (
761-
CategoricalDtype,
762-
is_datetime64_any_dtype,
763-
)
764-
from pandas.core.arrays import Categorical
765-
766760
dtype = pandas_dtype(dtype)
767761
if dtype == self.dtype:
768762
if not copy:
769763
return self
770764
else:
771765
return self.copy()
772766

773-
if (
774-
isinstance(self, Categorical)
775-
and isinstance(dtype, CategoricalDtype)
776-
and is_datetime64_any_dtype(self.categories)
777-
):
778-
return Categorical(self.to_numpy(), dtype=dtype)
779-
780767
if isinstance(dtype, ExtensionDtype):
781768
cls = dtype.construct_array_type()
782769
return cls._from_sequence(self, dtype=dtype, copy=copy)

pandas/core/arrays/categorical.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@
7373
NDArrayBackedExtensionArray,
7474
ravel_compat,
7575
)
76+
from pandas.core.arrays.arrow.array import ArrowExtensionArray
7677
from pandas.core.base import (
7778
ExtensionArray,
7879
NoNewAttributesMixin,
@@ -483,6 +484,18 @@ def __init__(
483484
)
484485

485486
else:
487+
if isinstance(values, ArrowExtensionArray):
488+
from pandas.api.types import (
489+
is_datetime64_any_dtype,
490+
is_timedelta64_dtype,
491+
)
492+
493+
cat_dtype = dtype.categories.dtype
494+
if is_datetime64_any_dtype(cat_dtype) or is_timedelta64_dtype(
495+
cat_dtype
496+
):
497+
values = values.to_numpy()
498+
486499
codes = _get_codes_for_values(values, dtype.categories)
487500

488501
if null_mask.any():

0 commit comments

Comments
 (0)