Skip to content

Commit 5987952

Browse files
committed
Updated conditions
1 parent 2dc0698 commit 5987952

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

pandas/core/arrays/base.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -757,7 +757,11 @@ def astype(self, dtype: AstypeArg, copy: bool = True) -> ArrayLike:
757757
>>> arr2.dtype
758758
dtype('float64')
759759
"""
760-
from pandas.api.types import CategoricalDtype
760+
from pandas.api.types import (
761+
CategoricalDtype,
762+
is_datetime64_any_dtype,
763+
)
764+
from pandas.core.arrays import Categorical
761765

762766
dtype = pandas_dtype(dtype)
763767
if dtype == self.dtype:
@@ -766,9 +770,11 @@ def astype(self, dtype: AstypeArg, copy: bool = True) -> ArrayLike:
766770
else:
767771
return self.copy()
768772

769-
if isinstance(dtype, CategoricalDtype):
770-
from pandas.core.arrays import Categorical
771-
773+
if (
774+
isinstance(self, Categorical)
775+
and isinstance(dtype, CategoricalDtype)
776+
and is_datetime64_any_dtype(self.categories)
777+
):
772778
return Categorical(self.to_numpy(), dtype=dtype)
773779

774780
if isinstance(dtype, ExtensionDtype):

0 commit comments

Comments
 (0)