Skip to content

Commit b5c1da0

Browse files
jbrockmendelKevin D Smith
authored and
Kevin D Smith
committed
REF: share more EA methods (pandas-dev#36209)
1 parent 83ce781 commit b5c1da0

File tree

4 files changed

+26
-59
lines changed

4 files changed

+26
-59
lines changed

pandas/core/arrays/_mixins.py

+9-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from pandas.errors import AbstractMethodError
77
from pandas.util._decorators import cache_readonly, doc
88

9-
from pandas.core.algorithms import searchsorted, take, unique
9+
from pandas.core.algorithms import take, unique
1010
from pandas.core.array_algos.transforms import shift
1111
from pandas.core.arrays.base import ExtensionArray
1212

@@ -102,6 +102,9 @@ def T(self: _T) -> _T:
102102

103103
# ------------------------------------------------------------------------
104104

105+
def _values_for_argsort(self):
106+
return self._ndarray
107+
105108
def copy(self: _T) -> _T:
106109
new_data = self._ndarray.copy()
107110
return self._from_backing_data(new_data)
@@ -135,7 +138,11 @@ def _concat_same_type(cls, to_concat, axis: int = 0):
135138

136139
@doc(ExtensionArray.searchsorted)
137140
def searchsorted(self, value, side="left", sorter=None):
138-
return searchsorted(self._ndarray, value, side=side, sorter=sorter)
141+
value = self._validate_searchsorted_value(value)
142+
return self._ndarray.searchsorted(value, side=side, sorter=sorter)
143+
144+
def _validate_searchsorted_value(self, value):
145+
return value
139146

140147
@doc(ExtensionArray.shift)
141148
def shift(self, periods=1, fill_value=None, axis=0):

pandas/core/arrays/categorical.py

+3-16
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from pandas._libs import NaT, algos as libalgos, hashtable as htable, lib
1313
from pandas._typing import ArrayLike, Dtype, Ordered, Scalar
1414
from pandas.compat.numpy import function as nv
15-
from pandas.util._decorators import cache_readonly, deprecate_kwarg, doc
15+
from pandas.util._decorators import cache_readonly, deprecate_kwarg
1616
from pandas.util._validators import validate_bool_kwarg, validate_fillna_kwargs
1717

1818
from pandas.core.dtypes.cast import (
@@ -45,12 +45,7 @@
4545
import pandas.core.algorithms as algorithms
4646
from pandas.core.algorithms import _get_data_algo, factorize, take_1d, unique1d
4747
from pandas.core.arrays._mixins import NDArrayBackedExtensionArray
48-
from pandas.core.base import (
49-
ExtensionArray,
50-
NoNewAttributesMixin,
51-
PandasObject,
52-
_shared_docs,
53-
)
48+
from pandas.core.base import ExtensionArray, NoNewAttributesMixin, PandasObject
5449
import pandas.core.common as com
5550
from pandas.core.construction import array, extract_array, sanitize_array
5651
from pandas.core.indexers import check_array_indexer, deprecate_ndim_indexing
@@ -1315,11 +1310,6 @@ def memory_usage(self, deep=False):
13151310
"""
13161311
return self._codes.nbytes + self.dtype.categories.memory_usage(deep=deep)
13171312

1318-
@doc(_shared_docs["searchsorted"], klass="Categorical")
1319-
def searchsorted(self, value, side="left", sorter=None):
1320-
value = self._validate_searchsorted_value(value)
1321-
return self.codes.searchsorted(value, side=side, sorter=sorter)
1322-
13231313
def isna(self):
13241314
"""
13251315
Detect missing values
@@ -1428,9 +1418,6 @@ def check_for_ordered(self, op):
14281418
"Categorical to an ordered one\n"
14291419
)
14301420

1431-
def _values_for_argsort(self):
1432-
return self._codes
1433-
14341421
def argsort(self, ascending=True, kind="quicksort", **kwargs):
14351422
"""
14361423
Return the indices that would sort the Categorical.
@@ -1879,7 +1866,7 @@ def __getitem__(self, key):
18791866
if result.ndim > 1:
18801867
deprecate_ndim_indexing(result)
18811868
return result
1882-
return self._constructor(result, dtype=self.dtype, fastpath=True)
1869+
return self._from_backing_data(result)
18831870

18841871
def __setitem__(self, key, value):
18851872
"""

pandas/core/arrays/datetimelike.py

+5-33
Original file line numberDiff line numberDiff line change
@@ -545,15 +545,18 @@ def __getitem__(self, key):
545545
result = self._ndarray[key]
546546
if self.ndim == 1:
547547
return self._box_func(result)
548-
return self._simple_new(result, dtype=self.dtype)
548+
return self._from_backing_data(result)
549549

550550
key = self._validate_getitem_key(key)
551551
result = self._ndarray[key]
552552
if lib.is_scalar(result):
553553
return self._box_func(result)
554554

555+
result = self._from_backing_data(result)
556+
555557
freq = self._get_getitem_freq(key)
556-
return self._simple_new(result, dtype=self.dtype, freq=freq)
558+
result._freq = freq
559+
return result
557560

558561
def _validate_getitem_key(self, key):
559562
if com.is_bool_indexer(key):
@@ -714,9 +717,6 @@ def _values_for_factorize(self):
714717
def _from_factorized(cls, values, original):
715718
return cls(values, dtype=original.dtype)
716719

717-
def _values_for_argsort(self):
718-
return self._ndarray
719-
720720
# ------------------------------------------------------------------
721721
# Validation Methods
722722
# TODO: try to de-duplicate these, ensure identical behavior
@@ -917,34 +917,6 @@ def _unbox(self, other, setitem: bool = False) -> Union[np.int64, np.ndarray]:
917917
# These are not part of the EA API, but we implement them because
918918
# pandas assumes they're there.
919919

920-
def searchsorted(self, value, side="left", sorter=None):
921-
"""
922-
Find indices where elements should be inserted to maintain order.
923-
924-
Find the indices into a sorted array `self` such that, if the
925-
corresponding elements in `value` were inserted before the indices,
926-
the order of `self` would be preserved.
927-
928-
Parameters
929-
----------
930-
value : array_like
931-
Values to insert into `self`.
932-
side : {'left', 'right'}, optional
933-
If 'left', the index of the first suitable location found is given.
934-
If 'right', return the last such index. If there is no suitable
935-
index, return either 0 or N (where N is the length of `self`).
936-
sorter : 1-D array_like, optional
937-
Optional array of integer indices that sort `self` into ascending
938-
order. They are typically the result of ``np.argsort``.
939-
940-
Returns
941-
-------
942-
indices : array of ints
943-
Array of insertion points with the same shape as `value`.
944-
"""
945-
value = self._validate_searchsorted_value(value)
946-
return self._data.searchsorted(value, side=side, sorter=sorter)
947-
948920
def value_counts(self, dropna=False):
949921
"""
950922
Return a Series containing counts of unique values.

pandas/core/arrays/numpy_.py

+9-8
Original file line numberDiff line numberDiff line change
@@ -260,15 +260,19 @@ def __getitem__(self, item):
260260
return result
261261

262262
def __setitem__(self, key, value) -> None:
263-
value = extract_array(value, extract_numpy=True)
263+
key = self._validate_setitem_key(key)
264+
value = self._validate_setitem_value(value)
265+
self._ndarray[key] = value
264266

265-
key = check_array_indexer(self, key)
266-
scalar_value = lib.is_scalar(value)
267+
def _validate_setitem_value(self, value):
268+
value = extract_array(value, extract_numpy=True)
267269

268-
if not scalar_value:
270+
if not lib.is_scalar(value):
269271
value = np.asarray(value, dtype=self._ndarray.dtype)
272+
return value
270273

271-
self._ndarray[key] = value
274+
def _validate_setitem_key(self, key):
275+
return check_array_indexer(self, key)
272276

273277
def isna(self) -> np.ndarray:
274278
return isna(self._ndarray)
@@ -308,9 +312,6 @@ def _validate_fill_value(self, fill_value):
308312
fill_value = self.dtype.na_value
309313
return fill_value
310314

311-
def _values_for_argsort(self) -> np.ndarray:
312-
return self._ndarray
313-
314315
def _values_for_factorize(self) -> Tuple[np.ndarray, int]:
315316
return self._ndarray, -1
316317

0 commit comments

Comments
 (0)