-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
REF: move sharable methods to ExtensionIndex #30717
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
Changes from all commits
95eed25
3e39309
3dbf7f6
f9d6027
40ca860
785a27c
90c2657
c48e2d3
9fc5c31
45f0c46
be7eef6
0ea90b9
6264814
1a5387a
f8832ed
b0f52a5
ef38914
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,8 @@ | |
""" | ||
from typing import List | ||
|
||
import numpy as np | ||
|
||
from pandas.compat.numpy import function as nv | ||
from pandas.util._decorators import cache_readonly | ||
|
||
|
@@ -170,6 +172,29 @@ class ExtensionIndex(Index): | |
__le__ = _make_wrapped_comparison_op("__le__") | ||
__ge__ = _make_wrapped_comparison_op("__ge__") | ||
|
||
def __getitem__(self, key): | ||
result = self._data[key] | ||
if isinstance(result, type(self._data)): | ||
return type(self)(result, name=self.name) | ||
|
||
# Includes cases where we get a 2D ndarray back for MPL compat | ||
return result | ||
|
||
def __iter__(self): | ||
return self._data.__iter__() | ||
|
||
@property | ||
def _ndarray_values(self) -> np.ndarray: | ||
return self._data._ndarray_values | ||
|
||
def dropna(self, how="any"): | ||
if how not in ("any", "all"): | ||
raise ValueError(f"invalid how option: {how}") | ||
|
||
if self.hasnans: | ||
return self._shallow_copy(self._data[~self._isnan]) | ||
return self._shallow_copy() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why are you overwriting the base Index one? Also, this dropped the docstring. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Because the base class uses ._values, where we want ._data here There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. But There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess. Past-me must have thought it not-obvious that this would always hold. If it can be removed, go for it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Did you also see my docstring comment? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I did. In this case I think removing the method makes sense. More generally I wonder if we can use a metaclass or something to automatically inherit docstrings and remove a lot of boilerplate (cc @bashtage IIRC you do something like this in arch) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe open a new issue to see if we can do this smarter? But for 1.0.0, I would just add back the docstring |
||
|
||
def repeat(self, repeats, axis=None): | ||
nv.validate_repeat(tuple(), dict(axis=axis)) | ||
result = self._data.repeat(repeats, axis=axis) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we use a faster constructor (simple_new ?) when we just want to wrap the correct type of ExtensionArray in the index?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that'd work. IIRC there were some corner cases involving CategoricalIndex.dtype, not sure if those are relevant here