Skip to content

Commit 35d5654

Browse files
authored
CLN: share setitem/getitem validators (#36619)
1 parent 422030b commit 35d5654

File tree

3 files changed

+4
-17
lines changed

3 files changed

+4
-17
lines changed

pandas/core/arrays/_mixins.py

+2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from pandas.core.algorithms import take, unique
1515
from pandas.core.array_algos.transforms import shift
1616
from pandas.core.arrays.base import ExtensionArray
17+
from pandas.core.construction import extract_array
1718
from pandas.core.indexers import check_array_indexer
1819

1920
_T = TypeVar("_T", bound="NDArrayBackedExtensionArray")
@@ -197,6 +198,7 @@ def __getitem__(self, key):
197198
return result
198199

199200
def _validate_getitem_key(self, key):
201+
key = extract_array(key, extract_numpy=True)
200202
return check_array_indexer(self, key)
201203

202204
@doc(ExtensionArray.fillna)

pandas/core/arrays/categorical.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
from pandas.core.base import ExtensionArray, NoNewAttributesMixin, PandasObject
4848
import pandas.core.common as com
4949
from pandas.core.construction import array, extract_array, sanitize_array
50-
from pandas.core.indexers import check_array_indexer, deprecate_ndim_indexing
50+
from pandas.core.indexers import deprecate_ndim_indexing
5151
from pandas.core.missing import interpolate_2d
5252
from pandas.core.ops.common import unpack_zerodim_and_defer
5353
from pandas.core.sorting import nargsort
@@ -1923,8 +1923,7 @@ def _validate_setitem_key(self, key):
19231923

19241924
# else: array of True/False in Series or Categorical
19251925

1926-
key = check_array_indexer(self, key)
1927-
return key
1926+
return super()._validate_setitem_key(key)
19281927

19291928
def _reverse_indexer(self) -> Dict[Hashable, np.ndarray]:
19301929
"""

pandas/core/arrays/numpy_.py

-14
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
from pandas.core.array_algos import masked_reductions
1717
from pandas.core.arrays._mixins import NDArrayBackedExtensionArray
1818
from pandas.core.arrays.base import ExtensionOpsMixin
19-
from pandas.core.construction import extract_array
2019

2120

2221
class PandasDtype(ExtensionDtype):
@@ -244,19 +243,6 @@ def __array_ufunc__(self, ufunc, method: str, *inputs, **kwargs):
244243
# ------------------------------------------------------------------------
245244
# Pandas ExtensionArray Interface
246245

247-
def _validate_getitem_key(self, key):
248-
if isinstance(key, type(self)):
249-
key = key._ndarray
250-
251-
return super()._validate_getitem_key(key)
252-
253-
def _validate_setitem_value(self, value):
254-
value = extract_array(value, extract_numpy=True)
255-
256-
if not lib.is_scalar(value):
257-
value = np.asarray(value, dtype=self._ndarray.dtype)
258-
return value
259-
260246
def isna(self) -> np.ndarray:
261247
return isna(self._ndarray)
262248

0 commit comments

Comments
 (0)