Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
5 changes: 0 additions & 5 deletions pandas/core/indexes/category.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
11 changes: 7 additions & 4 deletions pandas/core/indexes/extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)

Expand Down
10 changes: 1 addition & 9 deletions pandas/core/internals/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down