Skip to content

REF: share code for __setitem__ #36366

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 1 commit into from
Sep 15, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
12 changes: 12 additions & 0 deletions pandas/core/arrays/_mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from pandas.core.algorithms import take, unique
from pandas.core.array_algos.transforms import shift
from pandas.core.arrays.base import ExtensionArray
from pandas.core.indexers import check_array_indexer

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

Expand Down Expand Up @@ -156,3 +157,14 @@ def _validate_shift_value(self, fill_value):
# TODO: after deprecation in datetimelikearraymixin is enforced,
# we can remove this and ust validate_fill_value directly
return self._validate_fill_value(fill_value)

def __setitem__(self, key, value):
key = self._validate_setitem_key(key)
value = self._validate_setitem_value(value)
self._ndarray[key] = value

def _validate_setitem_key(self, key):
return check_array_indexer(self, key)

def _validate_setitem_value(self, value):
return value
14 changes: 0 additions & 14 deletions pandas/core/arrays/categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -1884,20 +1884,6 @@ def __getitem__(self, key):
return result
return self._from_backing_data(result)

def __setitem__(self, key, value):
"""
Item assignment.

Raises
------
ValueError
If (one or more) Value is not in categories or if a assigned
`Categorical` does not have the same categories
"""
key = self._validate_setitem_key(key)
value = self._validate_setitem_value(value)
self._ndarray[key] = value

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

Expand Down
4 changes: 1 addition & 3 deletions pandas/core/arrays/datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -609,9 +609,7 @@ def __setitem__(
if no_op:
return

value = self._validate_setitem_value(value)
key = check_array_indexer(self, key)
self._ndarray[key] = value
super().__setitem__(key, value)
self._maybe_clear_freq()

def _maybe_clear_freq(self):
Expand Down
8 changes: 0 additions & 8 deletions pandas/core/arrays/numpy_.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,21 +259,13 @@ def __getitem__(self, item):
result = type(self)(result)
return result

def __setitem__(self, key, value) -> None:
key = self._validate_setitem_key(key)
value = self._validate_setitem_value(value)
self._ndarray[key] = value

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

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

def _validate_setitem_key(self, key):
return check_array_indexer(self, key)

def isna(self) -> np.ndarray:
return isna(self._ndarray)

Expand Down