Skip to content

Commit

Permalink
CLN: remove rebox_native (#37608)
Browse files Browse the repository at this point in the history
  • Loading branch information
jbrockmendel authored Nov 3, 2020
1 parent 67b087f commit 7d40d3e
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 22 deletions.
5 changes: 3 additions & 2 deletions pandas/core/arrays/datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,9 @@ def _rebox_native(cls, value: int) -> Union[int, np.datetime64, np.timedelta64]:
"""
raise AbstractMethodError(cls)

def _unbox_scalar(self, value: DTScalarOrNaT, setitem: bool = False) -> int:
def _unbox_scalar(
self, value: DTScalarOrNaT, setitem: bool = False
) -> Union[np.int64, np.datetime64, np.timedelta64]:
"""
Unbox the integer value of a scalar `value`.
Expand Down Expand Up @@ -636,7 +638,6 @@ def _unbox(
"""
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)
Expand Down
9 changes: 3 additions & 6 deletions pandas/core/arrays/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -454,16 +454,13 @@ def _generate_range(
# -----------------------------------------------------------------
# DatetimeLike Interface

@classmethod
def _rebox_native(cls, value: int) -> np.datetime64:
return np.int64(value).view("M8[ns]")

def _unbox_scalar(self, value, setitem: bool = False):
def _unbox_scalar(self, value, setitem: bool = False) -> np.datetime64:
if not isinstance(value, self._scalar_type) and value is not NaT:
raise ValueError("'value' should be a Timestamp.")
if not isna(value):
self._check_compatible_with(value, setitem=setitem)
return value.value
return value.asm8
return np.datetime64(value.value, "ns")

def _scalar_from_string(self, value):
return Timestamp(value, tz=self.tz)
Expand Down
8 changes: 2 additions & 6 deletions pandas/core/arrays/period.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,18 +260,14 @@ def _generate_range(cls, start, end, periods, freq, fields):
# -----------------------------------------------------------------
# DatetimeLike Interface

@classmethod
def _rebox_native(cls, value: int) -> np.int64:
return np.int64(value)

def _unbox_scalar(
self, value: Union[Period, NaTType], setitem: bool = False
) -> int:
if value is NaT:
return value.value
return np.int64(value.value)
elif isinstance(value, self._scalar_type):
self._check_compatible_with(value, setitem=setitem)
return value.ordinal
return np.int64(value.ordinal)
else:
raise ValueError(f"'value' should be a Period. Got '{value}' instead.")

Expand Down
8 changes: 2 additions & 6 deletions pandas/core/arrays/timedeltas.py
Original file line number Diff line number Diff line change
Expand Up @@ -301,15 +301,11 @@ def _generate_range(cls, start, end, periods, freq, closed=None):
# ----------------------------------------------------------------
# DatetimeLike Interface

@classmethod
def _rebox_native(cls, value: int) -> np.timedelta64:
return np.int64(value).view("m8[ns]")

def _unbox_scalar(self, value, setitem: bool = False):
def _unbox_scalar(self, value, setitem: bool = False) -> np.timedelta64:
if not isinstance(value, self._scalar_type) and value is not NaT:
raise ValueError("'value' should be a Timedelta.")
self._check_compatible_with(value, setitem=setitem)
return value.value
return np.timedelta64(value.value, "ns")

def _scalar_from_string(self, value):
return Timedelta(value)
Expand Down
5 changes: 3 additions & 2 deletions pandas/tests/arrays/test_datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,10 +191,11 @@ def test_unbox_scalar(self):
data = np.arange(10, dtype="i8") * 24 * 3600 * 10 ** 9
arr = self.array_cls(data, freq="D")
result = arr._unbox_scalar(arr[0])
assert isinstance(result, int)
expected = arr._data.dtype.type
assert isinstance(result, expected)

result = arr._unbox_scalar(pd.NaT)
assert isinstance(result, int)
assert isinstance(result, expected)

msg = f"'value' should be a {self.dtype.__name__}."
with pytest.raises(ValueError, match=msg):
Expand Down

0 comments on commit 7d40d3e

Please sign in to comment.