From 85cdbd260b7616d6a95b9e5d2e85f9450e9d9e00 Mon Sep 17 00:00:00 2001 From: Brock Date: Wed, 21 Oct 2020 08:33:23 -0700 Subject: [PATCH] CLN: always rebox_native --- pandas/core/arrays/datetimelike.py | 29 +++++++++++++---------------- pandas/core/indexes/datetimelike.py | 13 ++++++------- 2 files changed, 19 insertions(+), 23 deletions(-) diff --git a/pandas/core/arrays/datetimelike.py b/pandas/core/arrays/datetimelike.py index f2a0173c0d593..03cf97bde774e 100644 --- a/pandas/core/arrays/datetimelike.py +++ b/pandas/core/arrays/datetimelike.py @@ -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 @@ -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): """ @@ -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 = ( @@ -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)}" @@ -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 # ------------------------------------------------------------------ @@ -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): diff --git a/pandas/core/indexes/datetimelike.py b/pandas/core/indexes/datetimelike.py index f67d1ec0aa65d..76d001d2f3ce6 100644 --- a/pandas/core/indexes/datetimelike.py +++ b/pandas/core/indexes/datetimelike.py @@ -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: @@ -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]