Skip to content

Commit 43cfb0b

Browse files
jbrockmendelJulianWgs
authored andcommitted
CLN: always rebox_native (pandas-dev#37313)
1 parent 91c46c4 commit 43cfb0b

File tree

2 files changed

+19
-23
lines changed

2 files changed

+19
-23
lines changed

pandas/core/arrays/datetimelike.py

+13-16
Original file line numberDiff line numberDiff line change
@@ -480,8 +480,7 @@ def _validate_fill_value(self, fill_value):
480480
fill_value = self._validate_scalar(fill_value, msg)
481481
except TypeError as err:
482482
raise ValueError(msg) from err
483-
rv = self._unbox(fill_value)
484-
return self._rebox_native(rv)
483+
return self._unbox(fill_value)
485484

486485
def _validate_shift_value(self, fill_value):
487486
# TODO(2.0): once this deprecation is enforced, use _validate_fill_value
@@ -508,8 +507,7 @@ def _validate_shift_value(self, fill_value):
508507
)
509508
fill_value = new_fill
510509

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

514512
def _validate_scalar(self, value, msg: Optional[str] = None):
515513
"""
@@ -591,8 +589,7 @@ def _validate_searchsorted_value(self, value):
591589
else:
592590
value = self._validate_listlike(value)
593591

594-
rv = self._unbox(value)
595-
return self._rebox_native(rv)
592+
return self._unbox(value)
596593

597594
def _validate_setitem_value(self, value):
598595
msg = (
@@ -604,15 +601,13 @@ def _validate_setitem_value(self, value):
604601
else:
605602
value = self._validate_scalar(value, msg)
606603

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

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

614-
rv = self._unbox(value, setitem=True)
615-
return self._rebox_native(rv)
610+
return self._unbox(value, setitem=True)
616611

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

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

627-
def _unbox(self, other, setitem: bool = False) -> Union[np.int64, np.ndarray]:
621+
def _unbox(
622+
self, other, setitem: bool = False
623+
) -> Union[np.int64, np.datetime64, np.timedelta64, np.ndarray]:
628624
"""
629625
Unbox either a scalar with _unbox_scalar or an instance of our own type.
630626
"""
631627
if lib.is_scalar(other):
632628
other = self._unbox_scalar(other, setitem=setitem)
629+
other = self._rebox_native(other)
633630
else:
634631
# same type as self
635632
self._check_compatible_with(other, setitem=setitem)
636-
other = other.view("i8")
633+
other = other._ndarray
637634
return other
638635

639636
# ------------------------------------------------------------------
@@ -862,8 +859,8 @@ def _cmp_method(self, other, op):
862859
)
863860
return result
864861

865-
other_i8 = self._unbox(other)
866-
result = op(self.asi8, other_i8)
862+
other_vals = self._unbox(other)
863+
result = op(self._ndarray, other_vals)
867864

868865
o_mask = isna(other)
869866
if self._hasnans | np.any(o_mask):

pandas/core/indexes/datetimelike.py

+6-7
Original file line numberDiff line numberDiff line change
@@ -412,8 +412,8 @@ def _partial_date_slice(
412412
self._validate_partial_date_slice(reso)
413413

414414
t1, t2 = self._parsed_string_to_bounds(reso, parsed)
415-
i8vals = self.asi8
416-
unbox = self._data._unbox_scalar
415+
vals = self._data._ndarray
416+
unbox = self._data._unbox
417417

418418
if self.is_monotonic:
419419

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

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

434433
else:
435-
lhs_mask = i8vals >= unbox(t1)
436-
rhs_mask = i8vals <= unbox(t2)
434+
lhs_mask = vals >= unbox(t1)
435+
rhs_mask = vals <= unbox(t2)
437436

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

0 commit comments

Comments
 (0)