Skip to content

Commit

Permalink
BUG: Fix failing hash funciton for certain non-ns resolution `Timedel…
Browse files Browse the repository at this point in the history
…ta`s (pandas-dev#54037)

* Add reproducing test

* Attempt fix

* Add GH reference

* Add whatsnew entry

* Pre-commit after the fact
  • Loading branch information
harahu authored and im-vinicius committed Jul 8, 2023
1 parent 43a405b commit 689620a
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 1 deletion.
1 change: 1 addition & 0 deletions doc/source/whatsnew/v2.1.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,7 @@ Timedelta
- :meth:`TimedeltaIndex.map` with ``na_action="ignore"`` now works as expected (:issue:`51644`)
- Bug in :class:`TimedeltaIndex` division or multiplication leading to ``.freq`` of "0 Days" instead of ``None`` (:issue:`51575`)
- Bug in :class:`Timedelta` with Numpy timedelta64 objects not properly raising ``ValueError`` (:issue:`52806`)
- Bug in :meth:`Timedelta.__hash__`, raising an ``OutOfBoundsTimedelta`` on certain large values of second resolution (:issue:`54037`)
- Bug in :meth:`Timedelta.round` with values close to the implementation bounds returning incorrect results instead of raising ``OutOfBoundsTimedelta`` (:issue:`51494`)
- Bug in :meth:`arrays.TimedeltaArray.map` and :meth:`TimedeltaIndex.map`, where the supplied callable operated array-wise instead of element-wise (:issue:`51977`)
-
Expand Down
2 changes: 1 addition & 1 deletion pandas/_libs/tslibs/timedeltas.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -1156,7 +1156,7 @@ cdef class _Timedelta(timedelta):
# resolution.
try:
obj = (<_Timedelta>self)._as_creso(<NPY_DATETIMEUNIT>(self._creso + 1))
except OverflowError:
except OutOfBoundsTimedelta:
# Doesn't fit, so we're off the hook
return hash(self._value)
else:
Expand Down
6 changes: 6 additions & 0 deletions pandas/tests/scalar/timedelta/test_timedelta.py
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,12 @@ def test_resolution(self, td):
assert result == expected
assert result._creso == expected._creso

def test_hash(self) -> None:
# GH#54037
second_resolution_max = Timedelta(0).as_unit("s").max

assert hash(second_resolution_max)


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

0 comments on commit 689620a

Please sign in to comment.