diff --git a/pandas/core/indexes/category.py b/pandas/core/indexes/category.py index ecf77a9987bee..059c36a901297 100644 --- a/pandas/core/indexes/category.py +++ b/pandas/core/indexes/category.py @@ -354,11 +354,6 @@ def __contains__(self, key: Any) -> bool: return contains(self, key, container=self._engine) - @doc(Index.astype) - def astype(self, dtype, copy=True): - res_data = self._data.astype(dtype, copy=copy) - return Index(res_data, name=self.name) - @doc(Index.fillna) def fillna(self, value, downcast=None): value = self._require_scalar(value) diff --git a/pandas/core/indexes/extension.py b/pandas/core/indexes/extension.py index 73f96b2f6ad41..661adde44089c 100644 --- a/pandas/core/indexes/extension.py +++ b/pandas/core/indexes/extension.py @@ -11,7 +11,7 @@ from pandas.errors import AbstractMethodError from pandas.util._decorators import cache_readonly, doc -from pandas.core.dtypes.common import is_dtype_equal, is_object_dtype +from pandas.core.dtypes.common import is_dtype_equal, is_object_dtype, pandas_dtype from pandas.core.dtypes.generic import ABCDataFrame, ABCSeries from pandas.core.arrays import ExtensionArray @@ -294,9 +294,12 @@ def map(self, mapper, na_action=None): @doc(Index.astype) def astype(self, dtype, copy=True): - if is_dtype_equal(self.dtype, dtype) and copy is False: - # Ensure that self.astype(self.dtype) is self - return self + dtype = pandas_dtype(dtype) + if is_dtype_equal(self.dtype, dtype): + if not copy: + # Ensure that self.astype(self.dtype) is self + return self + return self.copy() new_values = self._data.astype(dtype, copy=copy) diff --git a/pandas/core/internals/blocks.py b/pandas/core/internals/blocks.py index 0bd60931f9a7e..6500cfcd9ea5a 100644 --- a/pandas/core/internals/blocks.py +++ b/pandas/core/internals/blocks.py @@ -649,15 +649,7 @@ def astype(self, dtype, copy: bool = False, errors: str = "raise"): def _astype(self, dtype: DtypeObj, copy: bool) -> ArrayLike: values = self.values - if is_categorical_dtype(dtype): - - if is_categorical_dtype(values.dtype): - # GH#10696/GH#18593: update an existing categorical efficiently - return values.astype(dtype, copy=copy) - - return Categorical(values, dtype=dtype) - - elif is_datetime64tz_dtype(dtype) and is_datetime64_dtype(values.dtype): + if is_datetime64tz_dtype(dtype) and is_datetime64_dtype(values.dtype): # if we are passed a datetime64[ns, tz] if copy: # this should be the only copy