Skip to content

Commit 4a08c02

Browse files
authored
CLN: _almost_ always rebox_native following _unbox (pandas-dev#37297)
1 parent ce12627 commit 4a08c02

File tree

2 files changed

+13
-15
lines changed

2 files changed

+13
-15
lines changed

pandas/core/arrays/datetimelike.py

+8-9
Original file line numberDiff line numberDiff line change
@@ -508,7 +508,8 @@ def _validate_shift_value(self, fill_value):
508508
)
509509
fill_value = new_fill
510510

511-
return self._unbox(fill_value)
511+
rv = self._unbox(fill_value)
512+
return self._rebox_native(rv)
512513

513514
def _validate_scalar(self, value, msg: Optional[str] = None):
514515
"""
@@ -603,18 +604,15 @@ def _validate_setitem_value(self, value):
603604
else:
604605
value = self._validate_scalar(value, msg)
605606

606-
return self._unbox(value, setitem=True)
607+
rv = self._unbox(value, setitem=True)
608+
return self._rebox_native(rv)
607609

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

612-
self._check_compatible_with(value, setitem=True)
613-
# TODO: if we dont have compat, should we raise or astype(object)?
614-
# PeriodIndex does astype(object)
615-
return value
616-
# Note: we do not unbox here because the caller needs boxed value
617-
# to check for freq.
614+
rv = self._unbox(value, setitem=True)
615+
return self._rebox_native(rv)
618616

619617
def _validate_where_value(self, other):
620618
msg = f"Where requires matching dtype, not {type(other)}"
@@ -623,7 +621,8 @@ def _validate_where_value(self, other):
623621
else:
624622
other = self._validate_listlike(other)
625623

626-
return self._unbox(other, setitem=True)
624+
rv = self._unbox(other, setitem=True)
625+
return self._rebox_native(rv)
627626

628627
def _unbox(self, other, setitem: bool = False) -> Union[np.int64, np.ndarray]:
629628
"""

pandas/core/indexes/datetimelike.py

+5-6
Original file line numberDiff line numberDiff line change
@@ -484,7 +484,7 @@ def isin(self, values, level=None):
484484

485485
@Appender(Index.where.__doc__)
486486
def where(self, cond, other=None):
487-
values = self.view("i8")
487+
values = self._data._ndarray
488488

489489
try:
490490
other = self._data._validate_where_value(other)
@@ -493,7 +493,7 @@ def where(self, cond, other=None):
493493
oth = getattr(other, "dtype", other)
494494
raise TypeError(f"Where requires matching dtype, not {oth}") from err
495495

496-
result = np.where(cond, values, other).astype("i8")
496+
result = np.where(cond, values, other)
497497
arr = self._data._from_backing_data(result)
498498
return type(self)._simple_new(arr, name=self.name)
499499

@@ -610,7 +610,8 @@ def insert(self, loc: int, item):
610610
-------
611611
new_index : Index
612612
"""
613-
item = self._data._validate_insert_value(item)
613+
value = self._data._validate_insert_value(item)
614+
item = self._data._box_func(value)
614615

615616
freq = None
616617
if is_period_dtype(self.dtype):
@@ -630,10 +631,8 @@ def insert(self, loc: int, item):
630631
freq = self.freq
631632

632633
arr = self._data
633-
item = arr._unbox_scalar(item)
634-
item = arr._rebox_native(item)
635634

636-
new_values = np.concatenate([arr._ndarray[:loc], [item], arr._ndarray[loc:]])
635+
new_values = np.concatenate([arr._ndarray[:loc], [value], arr._ndarray[loc:]])
637636
new_arr = self._data._from_backing_data(new_values)
638637
new_arr._freq = freq
639638

0 commit comments

Comments
 (0)