Skip to content

BUG: Timedelta comparisons with very large pytimedeltas overflowing #51538

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 4 commits into from
Mar 27, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v2.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1216,6 +1216,7 @@ Timedelta
- Bug in :func:`to_timedelta` raising error when input has nullable dtype ``Float64`` (:issue:`48796`)
- Bug in :class:`Timedelta` constructor incorrectly raising instead of returning ``NaT`` when given a ``np.timedelta64("nat")`` (:issue:`48898`)
- Bug in :class:`Timedelta` constructor failing to raise when passed both a :class:`Timedelta` object and keywords (e.g. days, seconds) (:issue:`48898`)
- Bug in :class:`Timedelta` comparisons with very large ``datetime.timedelta`` objects incorrect raising ``OutOfBoundsTimedelta`` (:issue:`49021`)

Timezones
^^^^^^^^^
Expand Down
19 changes: 17 additions & 2 deletions pandas/_libs/tslibs/timedeltas.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import warnings
cimport cython
from cpython.object cimport (
Py_EQ,
Py_LE,
Py_LT,
Py_NE,
PyObject,
PyObject_RichCompare,
Expand Down Expand Up @@ -1149,8 +1151,21 @@ cdef class _Timedelta(timedelta):
if isinstance(other, _Timedelta):
ots = other
elif is_any_td_scalar(other):
ots = Timedelta(other)
# TODO: watch out for overflows
try:
ots = Timedelta(other)
except OutOfBoundsTimedelta as err:
# GH#49021 pytimedelta.max overflows
if not PyDelta_Check(other):
# TODO: handle this case
raise
if op == Py_EQ:
return False
elif op == Py_NE:
return True
elif op == Py_LE or op == Py_LT:
return self.days <= other.days
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Shouldn't we use <= or PY_LE and < for Py_LT?
  2. What about the other components?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm i wrote this with self.unit == "ns" in mind, in which case if it overflows we really just need to know which direction it overflowed. but with "s"/"ms" this could be subtly wrong. will update w/ tests.

else:
return self.days >= other.days

elif other is NaT:
return op == Py_NE
Expand Down
40 changes: 40 additions & 0 deletions pandas/tests/scalar/timedelta/test_arithmetic.py
Original file line number Diff line number Diff line change
Expand Up @@ -966,6 +966,46 @@ def test_td_op_timedelta_timedeltalike_array(self, op, arr):


class TestTimedeltaComparison:
def test_compare_pytimedelta_bounds(self):
# GH#49021 don't overflow on comparison with very large pytimedeltas

for unit in ["ns", "us"]:
tdmax = Timedelta.max.as_unit(unit).max
tdmin = Timedelta.min.as_unit(unit).min

assert tdmax < timedelta.max
assert tdmax <= timedelta.max
assert not tdmax > timedelta.max
assert not tdmax >= timedelta.max
assert tdmax != timedelta.max
assert not tdmax == timedelta.max

assert tdmin > timedelta.min
assert tdmin >= timedelta.min
assert not tdmin < timedelta.min
assert not tdmin <= timedelta.min
assert tdmin != timedelta.min
assert not tdmin == timedelta.min

# But the "ms" and "s"-reso bounds extend pass pytimedelta
for unit in ["ms", "s"]:
tdmax = Timedelta.max.as_unit(unit).max
tdmin = Timedelta.min.as_unit(unit).min

assert tdmax > timedelta.max
assert tdmax >= timedelta.max
assert not tdmax < timedelta.max
assert not tdmax <= timedelta.max
assert tdmax != timedelta.max
assert not tdmax == timedelta.max

assert tdmin < timedelta.min
assert tdmin <= timedelta.min
assert not tdmin > timedelta.min
assert not tdmin >= timedelta.min
assert tdmin != timedelta.min
assert not tdmin == timedelta.min

def test_compare_tick(self, tick_classes):
cls = tick_classes

Expand Down