Skip to content

CLN: always rebox_native in DatetimeLikeArray #37313

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
Oct 22, 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
29 changes: 13 additions & 16 deletions pandas/core/arrays/datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -480,8 +480,7 @@ def _validate_fill_value(self, fill_value):
fill_value = self._validate_scalar(fill_value, msg)
except TypeError as err:
raise ValueError(msg) from err
rv = self._unbox(fill_value)
return self._rebox_native(rv)
return self._unbox(fill_value)

def _validate_shift_value(self, fill_value):
# TODO(2.0): once this deprecation is enforced, use _validate_fill_value
Expand All @@ -508,8 +507,7 @@ def _validate_shift_value(self, fill_value):
)
fill_value = new_fill

rv = self._unbox(fill_value)
return self._rebox_native(rv)
return self._unbox(fill_value)

def _validate_scalar(self, value, msg: Optional[str] = None):
"""
Expand Down Expand Up @@ -591,8 +589,7 @@ def _validate_searchsorted_value(self, value):
else:
value = self._validate_listlike(value)

rv = self._unbox(value)
return self._rebox_native(rv)
return self._unbox(value)

def _validate_setitem_value(self, value):
msg = (
Expand All @@ -604,15 +601,13 @@ def _validate_setitem_value(self, value):
else:
value = self._validate_scalar(value, msg)

rv = self._unbox(value, setitem=True)
return self._rebox_native(rv)
return self._unbox(value, setitem=True)

def _validate_insert_value(self, value):
msg = f"cannot insert {type(self).__name__} with incompatible label"
value = self._validate_scalar(value, msg)

rv = self._unbox(value, setitem=True)
return self._rebox_native(rv)
return self._unbox(value, setitem=True)

def _validate_where_value(self, other):
msg = f"Where requires matching dtype, not {type(other)}"
Expand All @@ -621,19 +616,21 @@ def _validate_where_value(self, other):
else:
other = self._validate_listlike(other)

rv = self._unbox(other, setitem=True)
return self._rebox_native(rv)
return self._unbox(other, setitem=True)

def _unbox(self, other, setitem: bool = False) -> Union[np.int64, np.ndarray]:
def _unbox(
self, other, setitem: bool = False
) -> Union[np.int64, np.datetime64, np.timedelta64, 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, setitem=setitem)
other = self._rebox_native(other)
else:
# same type as self
self._check_compatible_with(other, setitem=setitem)
other = other.view("i8")
other = other._ndarray
return other

# ------------------------------------------------------------------
Expand Down Expand Up @@ -862,8 +859,8 @@ def _cmp_method(self, other, op):
)
return result

other_i8 = self._unbox(other)
result = op(self.asi8, other_i8)
other_vals = self._unbox(other)
result = op(self._ndarray, other_vals)

o_mask = isna(other)
if self._hasnans | np.any(o_mask):
Expand Down
13 changes: 6 additions & 7 deletions pandas/core/indexes/datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -412,8 +412,8 @@ def _partial_date_slice(
self._validate_partial_date_slice(reso)

t1, t2 = self._parsed_string_to_bounds(reso, parsed)
i8vals = self.asi8
unbox = self._data._unbox_scalar
vals = self._data._ndarray
unbox = self._data._unbox

if self.is_monotonic:

Expand All @@ -426,14 +426,13 @@ def _partial_date_slice(
# TODO: does this depend on being monotonic _increasing_?

# a monotonic (sorted) series can be sliced
# Use asi8.searchsorted to avoid re-validating Periods/Timestamps
left = i8vals.searchsorted(unbox(t1), side="left")
right = i8vals.searchsorted(unbox(t2), side="right")
left = vals.searchsorted(unbox(t1), side="left")
right = vals.searchsorted(unbox(t2), side="right")
return slice(left, right)

else:
lhs_mask = i8vals >= unbox(t1)
rhs_mask = i8vals <= unbox(t2)
lhs_mask = vals >= unbox(t1)
rhs_mask = vals <= unbox(t2)

# try to find the dates
return (lhs_mask & rhs_mask).nonzero()[0]
Expand Down