From fa84543933d7411fca7a834a1b1c09cdd211f64b Mon Sep 17 00:00:00 2001 From: Brock Date: Mon, 16 Nov 2020 20:31:44 -0800 Subject: [PATCH] CLN: defer CategoricalIndex.astype to Categorical.astype --- pandas/core/arrays/categorical.py | 8 ++++---- pandas/core/indexes/category.py | 18 ++---------------- 2 files changed, 6 insertions(+), 20 deletions(-) diff --git a/pandas/core/arrays/categorical.py b/pandas/core/arrays/categorical.py index 67818e6cf8fae..3dec27d4af1ce 100644 --- a/pandas/core/arrays/categorical.py +++ b/pandas/core/arrays/categorical.py @@ -405,12 +405,12 @@ def astype(self, dtype: Dtype, copy: bool = True) -> ArrayLike: if is_categorical_dtype(dtype): dtype = cast(Union[str, CategoricalDtype], dtype) - # GH 10696/18593 + # GH 10696/18593/18630 dtype = self.dtype.update_dtype(dtype) - self = self.copy() if copy else self + result = self.copy() if copy else self if dtype == self.dtype: - return self - return self._set_dtype(dtype) + return result + return result._set_dtype(dtype) if is_extension_array_dtype(dtype): return array(self, dtype=dtype, copy=copy) if is_integer_dtype(dtype) and self.isna().any(): diff --git a/pandas/core/indexes/category.py b/pandas/core/indexes/category.py index d944bd5ffad40..2d87799320902 100644 --- a/pandas/core/indexes/category.py +++ b/pandas/core/indexes/category.py @@ -14,10 +14,8 @@ from pandas.core.dtypes.common import ( ensure_platform_int, is_categorical_dtype, - is_interval_dtype, is_list_like, is_scalar, - pandas_dtype, ) from pandas.core.dtypes.dtypes import CategoricalDtype from pandas.core.dtypes.missing import is_valid_nat_for_dtype, isna, notna @@ -371,20 +369,8 @@ def __contains__(self, key: Any) -> bool: @doc(Index.astype) def astype(self, dtype, copy=True): - if dtype is not None: - dtype = pandas_dtype(dtype) - - if is_interval_dtype(dtype): - from pandas import IntervalIndex - - return IntervalIndex(np.array(self)) - elif is_categorical_dtype(dtype): - # GH 18630 - dtype = self.dtype.update_dtype(dtype) - if dtype == self.dtype: - return self.copy() if copy else self - - return Index.astype(self, dtype=dtype, copy=copy) + res_data = self._data.astype(dtype, copy=copy) + return Index(res_data, name=self.name) @doc(Index.fillna) def fillna(self, value, downcast=None):