Skip to content

REF: disallow ints from delta_to_nanoseconds #46378

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
Mar 16, 2022
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
6 changes: 3 additions & 3 deletions pandas/_libs/tslibs/timedeltas.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -173,11 +173,11 @@ cpdef int64_t delta_to_nanoseconds(delta) except? -1:
if is_tick_object(delta):
return delta.nanos
if isinstance(delta, _Timedelta):
delta = delta.value
return delta.value

if is_timedelta64_object(delta):
return get_timedelta64_value(ensure_td64ns(delta))
if is_integer_object(delta):
return delta

if PyDelta_Check(delta):
try:
return (
Expand Down
8 changes: 6 additions & 2 deletions pandas/core/arrays/datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -1316,7 +1316,9 @@ def __add__(self, other):
elif is_integer_dtype(other_dtype):
if not is_period_dtype(self.dtype):
raise integer_op_not_supported(self)
result = cast("PeriodArray", self)._addsub_int_array(other, operator.add)
result = cast("PeriodArray", self)._addsub_int_array_or_scalar(
other, operator.add
)
else:
# Includes Categorical, other ExtensionArrays
# For PeriodDtype, if self is a TimedeltaArray and other is a
Expand Down Expand Up @@ -1376,7 +1378,9 @@ def __sub__(self, other):
elif is_integer_dtype(other_dtype):
if not is_period_dtype(self.dtype):
raise integer_op_not_supported(self)
result = cast("PeriodArray", self)._addsub_int_array(other, operator.sub)
result = cast("PeriodArray", self)._addsub_int_array_or_scalar(
other, operator.sub
)
else:
# Includes ExtensionArrays, float_dtype
return NotImplemented
Expand Down
19 changes: 6 additions & 13 deletions pandas/core/arrays/period.py
Original file line number Diff line number Diff line change
Expand Up @@ -747,16 +747,16 @@ def _sub_period_array(self, other):
new_values[mask] = NaT
return new_values

def _addsub_int_array(
self, other: np.ndarray, op: Callable[[Any, Any], Any]
def _addsub_int_array_or_scalar(
self, other: np.ndarray | int, op: Callable[[Any, Any], Any]
) -> PeriodArray:
"""
Add or subtract array of integers; equivalent to applying
`_time_shift` pointwise.

Parameters
----------
other : np.ndarray[integer-dtype]
other : np.ndarray[int64] or int
op : {operator.add, operator.sub}

Returns
Expand All @@ -775,12 +775,7 @@ def _add_offset(self, other: BaseOffset):
assert not isinstance(other, Tick)

self._require_matching_freq(other, base=True)

# Note: when calling parent class's _add_timedeltalike_scalar,
# it will call delta_to_nanoseconds(delta). Because delta here
# is an integer, delta_to_nanoseconds will return it unchanged.
result = super()._add_timedeltalike_scalar(other.n)
return type(self)(result, freq=self.freq)
return self._addsub_int_array_or_scalar(other.n, operator.add)

def _add_timedeltalike_scalar(self, other):
"""
Expand All @@ -800,10 +795,8 @@ def _add_timedeltalike_scalar(self, other):
# special handling for np.timedelta64("NaT"), avoid calling
# _check_timedeltalike_freq_compat as that would raise TypeError
other = self._check_timedeltalike_freq_compat(other)
other = np.timedelta64(other, "ns")

# Note: when calling parent class's _add_timedeltalike_scalar,
# it will call delta_to_nanoseconds(delta). Because delta here
# is an integer, delta_to_nanoseconds will return it unchanged.
return super()._add_timedeltalike_scalar(other)

def _add_timedelta_arraylike(self, other):
Expand All @@ -828,7 +821,7 @@ def _add_timedelta_arraylike(self, other):
# all-NaT TimedeltaIndex is equivalent to a single scalar td64 NaT
return self + np.timedelta64("NaT")

ordinals = self._addsub_int_array(delta, operator.add).asi8
ordinals = self._addsub_int_array_or_scalar(delta, operator.add).asi8
return type(self)(ordinals, dtype=self.dtype)

def _check_timedeltalike_freq_compat(self, other):
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/indexes/period.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ def __new__(
def values(self) -> np.ndarray:
return np.asarray(self, dtype=object)

def _maybe_convert_timedelta(self, other):
def _maybe_convert_timedelta(self, other) -> int | npt.NDArray[np.int64]:
"""
Convert timedelta-like input to an integer multiple of self.freq

Expand Down
12 changes: 9 additions & 3 deletions pandas/tests/tslibs/test_timedeltas.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,6 @@
24 * 3600e9 + 111,
), # GH43764
(offsets.Nano(125), 125),
(1, 1),
(np.int64(2), 2),
(np.int32(3), 3),
],
)
def test_delta_to_nanoseconds(obj, expected):
Expand All @@ -46,6 +43,15 @@ def test_delta_to_nanoseconds_error():
with pytest.raises(TypeError, match="<class 'numpy.ndarray'>"):
delta_to_nanoseconds(obj)

with pytest.raises(TypeError, match="float"):
delta_to_nanoseconds(1.5)
with pytest.raises(TypeError, match="int"):
delta_to_nanoseconds(1)
with pytest.raises(TypeError, match="int"):
delta_to_nanoseconds(np.int64(2))
with pytest.raises(TypeError, match="int"):
delta_to_nanoseconds(np.int32(3))


def test_huge_nanoseconds_overflow():
# GH 32402
Expand Down