Skip to content

CLN: defer CategoricalIndex.astype to Categorical.astype #37908

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 17, 2020
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
8 changes: 4 additions & 4 deletions pandas/core/arrays/categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down
18 changes: 2 additions & 16 deletions pandas/core/indexes/category.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand Down