Skip to content

Commit bb929a6

Browse files
authored
CLN: defer CategoricalIndex.astype to Categorical.astype (#37908)
1 parent fead56f commit bb929a6

File tree

2 files changed

+6
-20
lines changed

2 files changed

+6
-20
lines changed

pandas/core/arrays/categorical.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -405,12 +405,12 @@ def astype(self, dtype: Dtype, copy: bool = True) -> ArrayLike:
405405
if is_categorical_dtype(dtype):
406406
dtype = cast(Union[str, CategoricalDtype], dtype)
407407

408-
# GH 10696/18593
408+
# GH 10696/18593/18630
409409
dtype = self.dtype.update_dtype(dtype)
410-
self = self.copy() if copy else self
410+
result = self.copy() if copy else self
411411
if dtype == self.dtype:
412-
return self
413-
return self._set_dtype(dtype)
412+
return result
413+
return result._set_dtype(dtype)
414414
if is_extension_array_dtype(dtype):
415415
return array(self, dtype=dtype, copy=copy)
416416
if is_integer_dtype(dtype) and self.isna().any():

pandas/core/indexes/category.py

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,8 @@
1414
from pandas.core.dtypes.common import (
1515
ensure_platform_int,
1616
is_categorical_dtype,
17-
is_interval_dtype,
1817
is_list_like,
1918
is_scalar,
20-
pandas_dtype,
2119
)
2220
from pandas.core.dtypes.dtypes import CategoricalDtype
2321
from pandas.core.dtypes.missing import is_valid_nat_for_dtype, isna, notna
@@ -370,20 +368,8 @@ def __contains__(self, key: Any) -> bool:
370368

371369
@doc(Index.astype)
372370
def astype(self, dtype, copy=True):
373-
if dtype is not None:
374-
dtype = pandas_dtype(dtype)
375-
376-
if is_interval_dtype(dtype):
377-
from pandas import IntervalIndex
378-
379-
return IntervalIndex(np.array(self))
380-
elif is_categorical_dtype(dtype):
381-
# GH 18630
382-
dtype = self.dtype.update_dtype(dtype)
383-
if dtype == self.dtype:
384-
return self.copy() if copy else self
385-
386-
return Index.astype(self, dtype=dtype, copy=copy)
371+
res_data = self._data.astype(dtype, copy=copy)
372+
return Index(res_data, name=self.name)
387373

388374
@doc(Index.fillna)
389375
def fillna(self, value, downcast=None):

0 commit comments

Comments
 (0)