Skip to content

Commit 74c352c

Browse files
authored
REF: share code for __setitem__ (#36366)
1 parent 947c8f2 commit 74c352c

File tree

4 files changed

+13
-25
lines changed

4 files changed

+13
-25
lines changed

pandas/core/arrays/_mixins.py

+12
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from pandas.core.algorithms import take, unique
1010
from pandas.core.array_algos.transforms import shift
1111
from pandas.core.arrays.base import ExtensionArray
12+
from pandas.core.indexers import check_array_indexer
1213

1314
_T = TypeVar("_T", bound="NDArrayBackedExtensionArray")
1415

@@ -156,3 +157,14 @@ def _validate_shift_value(self, fill_value):
156157
# TODO: after deprecation in datetimelikearraymixin is enforced,
157158
# we can remove this and ust validate_fill_value directly
158159
return self._validate_fill_value(fill_value)
160+
161+
def __setitem__(self, key, value):
162+
key = self._validate_setitem_key(key)
163+
value = self._validate_setitem_value(value)
164+
self._ndarray[key] = value
165+
166+
def _validate_setitem_key(self, key):
167+
return check_array_indexer(self, key)
168+
169+
def _validate_setitem_value(self, value):
170+
return value

pandas/core/arrays/categorical.py

-14
Original file line numberDiff line numberDiff line change
@@ -1894,20 +1894,6 @@ def __getitem__(self, key):
18941894
return result
18951895
return self._from_backing_data(result)
18961896

1897-
def __setitem__(self, key, value):
1898-
"""
1899-
Item assignment.
1900-
1901-
Raises
1902-
------
1903-
ValueError
1904-
If (one or more) Value is not in categories or if a assigned
1905-
`Categorical` does not have the same categories
1906-
"""
1907-
key = self._validate_setitem_key(key)
1908-
value = self._validate_setitem_value(value)
1909-
self._ndarray[key] = value
1910-
19111897
def _validate_setitem_value(self, value):
19121898
value = extract_array(value, extract_numpy=True)
19131899

pandas/core/arrays/datetimelike.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -609,9 +609,7 @@ def __setitem__(
609609
if no_op:
610610
return
611611

612-
value = self._validate_setitem_value(value)
613-
key = check_array_indexer(self, key)
614-
self._ndarray[key] = value
612+
super().__setitem__(key, value)
615613
self._maybe_clear_freq()
616614

617615
def _maybe_clear_freq(self):

pandas/core/arrays/numpy_.py

-8
Original file line numberDiff line numberDiff line change
@@ -259,21 +259,13 @@ def __getitem__(self, item):
259259
result = type(self)(result)
260260
return result
261261

262-
def __setitem__(self, key, value) -> None:
263-
key = self._validate_setitem_key(key)
264-
value = self._validate_setitem_value(value)
265-
self._ndarray[key] = value
266-
267262
def _validate_setitem_value(self, value):
268263
value = extract_array(value, extract_numpy=True)
269264

270265
if not lib.is_scalar(value):
271266
value = np.asarray(value, dtype=self._ndarray.dtype)
272267
return value
273268

274-
def _validate_setitem_key(self, key):
275-
return check_array_indexer(self, key)
276-
277269
def isna(self) -> np.ndarray:
278270
return isna(self._ndarray)
279271

0 commit comments

Comments
 (0)