Skip to content

Commit 58284dd

Browse files
committed
Added condition for CategoricalDtype
1 parent eb489f2 commit 58284dd

File tree

1 file changed

+7
-0
lines changed

1 file changed

+7
-0
lines changed

pandas/core/arrays/base.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -757,13 +757,20 @@ def astype(self, dtype: AstypeArg, copy: bool = True) -> ArrayLike:
757757
>>> arr2.dtype
758758
dtype('float64')
759759
"""
760+
from pandas.api.types import CategoricalDtype
761+
760762
dtype = pandas_dtype(dtype)
761763
if dtype == self.dtype:
762764
if not copy:
763765
return self
764766
else:
765767
return self.copy()
766768

769+
if isinstance(dtype, CategoricalDtype):
770+
from pandas.core.arrays import Categorical
771+
772+
return Categorical(self.to_numpy(), dtype=dtype)
773+
767774
if isinstance(dtype, ExtensionDtype):
768775
cls = dtype.construct_array_type()
769776
return cls._from_sequence(self, dtype=dtype, copy=copy)

0 commit comments

Comments
 (0)