Skip to content

Commit 689620a

Browse files
harahuim-vinicius
authored and
im-vinicius
committed
BUG: Fix failing hash funciton for certain non-ns resolution Timedeltas (pandas-dev#54037)
* Add reproducing test * Attempt fix * Add GH reference * Add whatsnew entry * Pre-commit after the fact
1 parent 43a405b commit 689620a

File tree

3 files changed

+8
-1
lines changed

3 files changed

+8
-1
lines changed

doc/source/whatsnew/v2.1.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,7 @@ Timedelta
393393
- :meth:`TimedeltaIndex.map` with ``na_action="ignore"`` now works as expected (:issue:`51644`)
394394
- Bug in :class:`TimedeltaIndex` division or multiplication leading to ``.freq`` of "0 Days" instead of ``None`` (:issue:`51575`)
395395
- Bug in :class:`Timedelta` with Numpy timedelta64 objects not properly raising ``ValueError`` (:issue:`52806`)
396+
- Bug in :meth:`Timedelta.__hash__`, raising an ``OutOfBoundsTimedelta`` on certain large values of second resolution (:issue:`54037`)
396397
- Bug in :meth:`Timedelta.round` with values close to the implementation bounds returning incorrect results instead of raising ``OutOfBoundsTimedelta`` (:issue:`51494`)
397398
- Bug in :meth:`arrays.TimedeltaArray.map` and :meth:`TimedeltaIndex.map`, where the supplied callable operated array-wise instead of element-wise (:issue:`51977`)
398399
-

pandas/_libs/tslibs/timedeltas.pyx

+1-1
Original file line numberDiff line numberDiff line change
@@ -1156,7 +1156,7 @@ cdef class _Timedelta(timedelta):
11561156
# resolution.
11571157
try:
11581158
obj = (<_Timedelta>self)._as_creso(<NPY_DATETIMEUNIT>(self._creso + 1))
1159-
except OverflowError:
1159+
except OutOfBoundsTimedelta:
11601160
# Doesn't fit, so we're off the hook
11611161
return hash(self._value)
11621162
else:

pandas/tests/scalar/timedelta/test_timedelta.py

+6
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,12 @@ def test_resolution(self, td):
305305
assert result == expected
306306
assert result._creso == expected._creso
307307

308+
def test_hash(self) -> None:
309+
# GH#54037
310+
second_resolution_max = Timedelta(0).as_unit("s").max
311+
312+
assert hash(second_resolution_max)
313+
308314

309315
def test_timedelta_class_min_max_resolution():
310316
# when accessed on the class (as opposed to an instance), we default

0 commit comments

Comments
 (0)