Skip to content

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

Merged
merged 4 commits into from
Sep 17, 2020
Merged
Show file tree
Hide file tree
Changes from 3 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
23 changes: 23 additions & 0 deletions pandas/core/arrays/_mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -30,6 +31,9 @@ def _from_backing_data(self: _T, arr: np.ndarray) -> _T:
"""
raise AbstractMethodError(self)

def _box_func(self, x):
Copy link
Member

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?

return x

# ------------------------------------------------------------------------

def take(
Expand Down Expand Up @@ -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):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this duplicates _validate_setitem_key. is this needed?

in what cases could one of a getitem and a setitem key be valid and other invalid?

Copy link
Member Author

Choose a reason for hiding this comment

The 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)
14 changes: 4 additions & 10 deletions pandas/core/arrays/categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -1882,17 +1882,11 @@ def __getitem__(self, key):
"""
Return an item.
"""
if isinstance(key, (int, np.integer)):
i = self._codes[key]
return self._box_func(i)

key = check_array_indexer(self, key)

result = self._codes[key]
if result.ndim > 1:
result = super().__getitem__(key)
if getattr(result, "ndim", 0) > 1:
result = result._ndarray
deprecate_ndim_indexing(result)
return result
return self._from_backing_data(result)
return result

def _validate_setitem_value(self, value):
value = extract_array(value, extract_numpy=True)
Expand Down
23 changes: 7 additions & 16 deletions pandas/core/arrays/datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -539,23 +539,11 @@ def __getitem__(self, key):
This getitem defers to the underlying array, which by-definition can
only handle list-likes, slices, and integer scalars
"""

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]
result = super().__getitem__(key)
if lib.is_scalar(result):
return self._box_func(result)

result = self._from_backing_data(result)
return result

freq = self._get_getitem_freq(key)
result._freq = freq
result._freq = self._get_getitem_freq(key)
return result

def _validate_getitem_key(self, key):
Expand All @@ -572,7 +560,7 @@ def _validate_getitem_key(self, key):
# this for now (would otherwise raise in check_array_indexer)
pass
else:
key = check_array_indexer(self, key)
key = super()._validate_getitem_key(key)
return key

def _get_getitem_freq(self, key):
Expand All @@ -582,7 +570,10 @@ def _get_getitem_freq(self, key):
is_period = is_period_dtype(self.dtype)
if is_period:
freq = self.freq
elif self.ndim != 1:
freq = None
else:
key = self._validate_getitem_key(key) # maybe ndarray[bool] -> slice
freq = None
if isinstance(key, slice):
if self.freq is not None and key.step is not None:
Expand Down
14 changes: 4 additions & 10 deletions pandas/core/arrays/numpy_.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
from pandas.core.arrays._mixins import NDArrayBackedExtensionArray
from pandas.core.arrays.base import ExtensionOpsMixin
from pandas.core.construction import extract_array
from pandas.core.indexers import check_array_indexer
from pandas.core.missing import backfill_1d, pad_1d


Expand Down Expand Up @@ -248,16 +247,11 @@ def __array_ufunc__(self, ufunc, method: str, *inputs, **kwargs):
# ------------------------------------------------------------------------
# Pandas ExtensionArray Interface

def __getitem__(self, item):
if isinstance(item, type(self)):
item = item._ndarray
def _validate_getitem_key(self, key):
if isinstance(key, type(self)):
key = key._ndarray

item = check_array_indexer(self, item)

result = self._ndarray[item]
if not lib.is_scalar(item):
result = type(self)(result)
return result
return super()._validate_getitem_key(key)

def _validate_setitem_value(self, value):
value = extract_array(value, extract_numpy=True)
Expand Down