Skip to content

REF: use only inherit_names in CategoricalIndex #42100

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
Jun 25, 2021
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
22 changes: 5 additions & 17 deletions pandas/core/indexes/category.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
notna,
)

from pandas.core import accessor
from pandas.core.arrays.categorical import (
Categorical,
contains,
Expand Down Expand Up @@ -63,9 +62,8 @@
],
Categorical,
)
@accessor.delegate_names(
delegate=Categorical,
accessors=[
@inherit_names(
[
"rename_categories",
"reorder_categories",
"add_categories",
Expand All @@ -75,10 +73,10 @@
"as_ordered",
"as_unordered",
],
typ="method",
overwrite=True,
Categorical,
wrap=True,
)
class CategoricalIndex(NDArrayBackedExtensionIndex, accessor.PandasDelegate):
class CategoricalIndex(NDArrayBackedExtensionIndex):
"""
Index based on an underlying :class:`Categorical`.

Expand Down Expand Up @@ -567,13 +565,3 @@ def _concat(self, to_concat: list[Index], name: Hashable) -> Index:
else:
cat = self._data._from_backing_data(codes)
return type(self)._simple_new(cat, name=name)

def _delegate_method(self, name: str, *args, **kwargs):
"""method delegation to the ._values"""
method = getattr(self._values, name)
if "inplace" in kwargs:
raise ValueError("cannot use inplace with CategoricalIndex")
res = method(*args, **kwargs)
if is_scalar(res):
return res
return CategoricalIndex(res, name=self.name)
2 changes: 2 additions & 0 deletions pandas/core/indexes/extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ def fset(self, value):
else:

def method(self, *args, **kwargs):
if "inplace" in kwargs:
raise ValueError(f"cannot use inplace with {type(self).__name__}")
result = attr(self._data, *args, **kwargs)
if wrap:
if isinstance(result, type(self._data)):
Expand Down