Skip to content

CLN: _almost_ always rebox_native following _unbox #37297

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 21, 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
17 changes: 8 additions & 9 deletions pandas/core/arrays/datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
"""
Expand Down Expand Up @@ -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)}"
Expand All @@ -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]:
"""
Expand Down
11 changes: 5 additions & 6 deletions pandas/core/indexes/datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)

Expand Down Expand Up @@ -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):
Expand All @@ -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

Expand Down