From 2c29f35623fdf020340cccafa872a1ca017b193d Mon Sep 17 00:00:00 2001 From: Brock Date: Tue, 20 Oct 2020 18:20:12 -0700 Subject: [PATCH] CLN: _almost_ always rebox_native following _unbox --- pandas/core/arrays/datetimelike.py | 17 ++++++++--------- pandas/core/indexes/datetimelike.py | 11 +++++------ 2 files changed, 13 insertions(+), 15 deletions(-) diff --git a/pandas/core/arrays/datetimelike.py b/pandas/core/arrays/datetimelike.py index de0a246861961..f2a0173c0d593 100644 --- a/pandas/core/arrays/datetimelike.py +++ b/pandas/core/arrays/datetimelike.py @@ -508,7 +508,8 @@ def _validate_shift_value(self, fill_value): ) fill_value = new_fill - return self._unbox(fill_value) + rv = self._unbox(fill_value) + return self._rebox_native(rv) def _validate_scalar(self, value, msg: Optional[str] = None): """ @@ -603,18 +604,15 @@ def _validate_setitem_value(self, value): else: value = self._validate_scalar(value, msg) - return self._unbox(value, setitem=True) + rv = self._unbox(value, setitem=True) + return self._rebox_native(rv) def _validate_insert_value(self, value): msg = f"cannot insert {type(self).__name__} with incompatible label" value = self._validate_scalar(value, msg) - self._check_compatible_with(value, setitem=True) - # 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. + rv = self._unbox(value, setitem=True) + return self._rebox_native(rv) def _validate_where_value(self, other): msg = f"Where requires matching dtype, not {type(other)}" @@ -623,7 +621,8 @@ def _validate_where_value(self, other): else: other = self._validate_listlike(other) - return self._unbox(other, setitem=True) + rv = self._unbox(other, setitem=True) + return self._rebox_native(rv) def _unbox(self, other, setitem: bool = False) -> Union[np.int64, np.ndarray]: """ diff --git a/pandas/core/indexes/datetimelike.py b/pandas/core/indexes/datetimelike.py index b6836a0bbe496..f67d1ec0aa65d 100644 --- a/pandas/core/indexes/datetimelike.py +++ b/pandas/core/indexes/datetimelike.py @@ -484,7 +484,7 @@ def isin(self, values, level=None): @Appender(Index.where.__doc__) def where(self, cond, other=None): - values = self.view("i8") + values = self._data._ndarray try: other = self._data._validate_where_value(other) @@ -493,7 +493,7 @@ def where(self, cond, other=None): oth = getattr(other, "dtype", other) raise TypeError(f"Where requires matching dtype, not {oth}") from err - result = np.where(cond, values, other).astype("i8") + result = np.where(cond, values, other) arr = self._data._from_backing_data(result) return type(self)._simple_new(arr, name=self.name) @@ -610,7 +610,8 @@ def insert(self, loc: int, item): ------- new_index : Index """ - item = self._data._validate_insert_value(item) + value = self._data._validate_insert_value(item) + item = self._data._box_func(value) freq = None if is_period_dtype(self.dtype): @@ -630,10 +631,8 @@ def insert(self, loc: int, item): freq = self.freq arr = self._data - item = arr._unbox_scalar(item) - item = arr._rebox_native(item) - new_values = np.concatenate([arr._ndarray[:loc], [item], arr._ndarray[loc:]]) + new_values = np.concatenate([arr._ndarray[:loc], [value], arr._ndarray[loc:]]) new_arr = self._data._from_backing_data(new_values) new_arr._freq = freq