Skip to content

REF: pass setitem to unbox_scalar to de-duplicate validation #36234

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 9, 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
20 changes: 11 additions & 9 deletions pandas/core/arrays/datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,14 +183,16 @@ def _rebox_native(cls, value: int) -> Union[int, np.datetime64, np.timedelta64]:
"""
raise AbstractMethodError(cls)

def _unbox_scalar(self, value: DTScalarOrNaT) -> int:
def _unbox_scalar(self, value: DTScalarOrNaT, setitem: bool = False) -> int:
"""
Unbox the integer value of a scalar `value`.

Parameters
----------
value : Period, Timestamp, Timedelta, or NaT
Depending on subclass.
setitem : bool, default False
Whether to check compatiblity with setitem strictness.

Returns
-------
Expand Down Expand Up @@ -841,6 +843,7 @@ def _validate_listlike(
if is_dtype_equal(value.categories.dtype, self.dtype):
# TODO: do we need equal dtype or just comparable?
value = value._internal_get_values()
value = extract_array(value, extract_numpy=True)

if allow_object and is_object_dtype(value.dtype):
pass
Expand Down Expand Up @@ -875,8 +878,7 @@ def _validate_setitem_value(self, value):
# TODO: cast_str for consistency?
value = self._validate_scalar(value, msg, cast_str=False)

self._check_compatible_with(value, setitem=True)
return self._unbox(value)
return self._unbox(value, setitem=True)

def _validate_insert_value(self, value):
msg = f"cannot insert {type(self).__name__} with incompatible label"
Expand All @@ -886,27 +888,27 @@ def _validate_insert_value(self, value):
# TODO: if we dont have compat, should we raise or astype(object)?
# PeriodIndex does astype(object)
return value
# Note: we do not unbox here because the caller needs boxed value
# to check for freq.

def _validate_where_value(self, other):
msg = f"Where requires matching dtype, not {type(other)}"
if not is_list_like(other):
other = self._validate_scalar(other, msg)
else:
other = self._validate_listlike(other, "where")
self._check_compatible_with(other, setitem=True)

self._check_compatible_with(other, setitem=True)
return self._unbox(other)
return self._unbox(other, setitem=True)

def _unbox(self, other) -> Union[np.int64, np.ndarray]:
def _unbox(self, other, setitem: bool = False) -> Union[np.int64, np.ndarray]:
"""
Unbox either a scalar with _unbox_scalar or an instance of our own type.
"""
if lib.is_scalar(other):
other = self._unbox_scalar(other)
other = self._unbox_scalar(other, setitem=setitem)
else:
# same type as self
self._check_compatible_with(other)
self._check_compatible_with(other, setitem=setitem)
other = other.view("i8")
return other

Expand Down
4 changes: 2 additions & 2 deletions pandas/core/arrays/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -451,11 +451,11 @@ def _generate_range(
def _rebox_native(cls, value: int) -> np.datetime64:
return np.int64(value).view("M8[ns]")

def _unbox_scalar(self, value):
def _unbox_scalar(self, value, setitem: bool = False):
if not isinstance(value, self._scalar_type) and value is not NaT:
raise ValueError("'value' should be a Timestamp.")
if not isna(value):
self._check_compatible_with(value)
self._check_compatible_with(value, setitem=setitem)
return value.value

def _scalar_from_string(self, value):
Expand Down
6 changes: 4 additions & 2 deletions pandas/core/arrays/period.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,11 +257,13 @@ def _generate_range(cls, start, end, periods, freq, fields):
def _rebox_native(cls, value: int) -> np.int64:
return np.int64(value)

def _unbox_scalar(self, value: Union[Period, NaTType]) -> int:
def _unbox_scalar(
self, value: Union[Period, NaTType], setitem: bool = False
) -> int:
if value is NaT:
return value.value
elif isinstance(value, self._scalar_type):
self._check_compatible_with(value)
self._check_compatible_with(value, setitem=setitem)
return value.ordinal
else:
raise ValueError(f"'value' should be a Period. Got '{value}' instead.")
Expand Down
4 changes: 2 additions & 2 deletions pandas/core/arrays/timedeltas.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,10 +283,10 @@ def _generate_range(cls, start, end, periods, freq, closed=None):
def _rebox_native(cls, value: int) -> np.timedelta64:
return np.int64(value).view("m8[ns]")

def _unbox_scalar(self, value):
def _unbox_scalar(self, value, setitem: bool = False):
if not isinstance(value, self._scalar_type) and value is not NaT:
raise ValueError("'value' should be a Timedelta.")
self._check_compatible_with(value)
self._check_compatible_with(value, setitem=setitem)
return value.value

def _scalar_from_string(self, value):
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/arrays/test_datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ def test_searchsorted(self):
# GH#29884 match numpy convention on whether NaT goes
# at the end or the beginning
result = arr.searchsorted(pd.NaT)
if _np_version_under1p18 or self.array_cls is PeriodArray:
if np_version_under1p18 or self.array_cls is PeriodArray:
# Following numpy convention, NaT goes at the beginning
# (unlike NaN which goes at the end)
assert result == 0
Expand Down