-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
REF: share __getitem__ for Categorical/PandasArray/DTA/TDA/PA #36391
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 3 commits
74931a2
783a5b8
685433a
4403ca6
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 |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
|
||
import numpy as np | ||
|
||
from pandas._libs import lib | ||
from pandas.compat.numpy import function as nv | ||
from pandas.errors import AbstractMethodError | ||
from pandas.util._decorators import cache_readonly, doc | ||
|
@@ -30,6 +31,9 @@ def _from_backing_data(self: _T, arr: np.ndarray) -> _T: | |
""" | ||
raise AbstractMethodError(self) | ||
|
||
def _box_func(self, x): | ||
return x | ||
|
||
# ------------------------------------------------------------------------ | ||
|
||
def take( | ||
|
@@ -168,3 +172,22 @@ def _validate_setitem_key(self, key): | |
|
||
def _validate_setitem_value(self, value): | ||
return value | ||
|
||
def __getitem__(self, key): | ||
if lib.is_integer(key): | ||
# fast-path | ||
result = self._ndarray[key] | ||
if self.ndim == 1: | ||
return self._box_func(result) | ||
return self._from_backing_data(result) | ||
|
||
key = self._validate_getitem_key(key) | ||
result = self._ndarray[key] | ||
if lib.is_scalar(result): | ||
return self._box_func(result) | ||
|
||
result = self._from_backing_data(result) | ||
return result | ||
|
||
def _validate_getitem_key(self, key): | ||
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. this duplicates in what cases could one of a getitem and a setitem key be valid and other invalid? 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. DatetimeLikeArrayMixin has special logic here. #36210 is about deprecating that |
||
return check_array_indexer(self, key) |
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.
Can you add a docstring for this function?