From 4a59abc1c8c8086aca5262ce2db0ae7b5e9f8a1f Mon Sep 17 00:00:00 2001 From: Harald Husum Date: Fri, 7 Jul 2023 16:07:18 +0200 Subject: [PATCH 1/5] Add reproducing test --- pandas/tests/scalar/timedelta/test_timedelta.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/pandas/tests/scalar/timedelta/test_timedelta.py b/pandas/tests/scalar/timedelta/test_timedelta.py index 722a68a1dce71..ab42eb6c8a22f 100644 --- a/pandas/tests/scalar/timedelta/test_timedelta.py +++ b/pandas/tests/scalar/timedelta/test_timedelta.py @@ -305,6 +305,11 @@ def test_resolution(self, td): assert result == expected assert result._creso == expected._creso + def test_hash(self) -> None: + 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 From 34b72a31b4828330d58941ec8f420415eee27f4e Mon Sep 17 00:00:00 2001 From: Harald Husum Date: Fri, 7 Jul 2023 16:46:39 +0200 Subject: [PATCH 2/5] Attempt fix --- pandas/_libs/tslibs/timedeltas.pyx | 2 +- pandas/tests/scalar/timedelta/test_timedelta.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/pandas/_libs/tslibs/timedeltas.pyx b/pandas/_libs/tslibs/timedeltas.pyx index 0981c966c4cd4..28aeb854638b6 100644 --- a/pandas/_libs/tslibs/timedeltas.pyx +++ b/pandas/_libs/tslibs/timedeltas.pyx @@ -1156,7 +1156,7 @@ cdef class _Timedelta(timedelta): # resolution. try: obj = (<_Timedelta>self)._as_creso((self._creso + 1)) - except OverflowError: + except OutOfBoundsTimedelta: # Doesn't fit, so we're off the hook return hash(self._value) else: diff --git a/pandas/tests/scalar/timedelta/test_timedelta.py b/pandas/tests/scalar/timedelta/test_timedelta.py index ab42eb6c8a22f..bd40e6ebcfac9 100644 --- a/pandas/tests/scalar/timedelta/test_timedelta.py +++ b/pandas/tests/scalar/timedelta/test_timedelta.py @@ -308,7 +308,7 @@ def test_resolution(self, td): def test_hash(self) -> None: second_resolution_max = Timedelta(0).as_unit("s").max - assert hash(second_resolution_max) + assert hash(second_resolution_max) == 42 def test_timedelta_class_min_max_resolution(): From 5e7deec5c997bbe61f8d2b98d72e5e1b9f29612f Mon Sep 17 00:00:00 2001 From: Harald Husum Date: Fri, 7 Jul 2023 17:24:17 +0200 Subject: [PATCH 3/5] Add GH reference --- pandas/tests/scalar/timedelta/test_timedelta.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pandas/tests/scalar/timedelta/test_timedelta.py b/pandas/tests/scalar/timedelta/test_timedelta.py index bd40e6ebcfac9..a83c6a8596575 100644 --- a/pandas/tests/scalar/timedelta/test_timedelta.py +++ b/pandas/tests/scalar/timedelta/test_timedelta.py @@ -306,9 +306,10 @@ def test_resolution(self, td): 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) == 42 + assert hash(second_resolution_max) def test_timedelta_class_min_max_resolution(): From db4ed342b9d07bcae7ae0640b62de53b762e380e Mon Sep 17 00:00:00 2001 From: Harald Husum Date: Fri, 7 Jul 2023 17:42:39 +0200 Subject: [PATCH 4/5] Add whatsnew entry --- doc/source/whatsnew/v2.1.0.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/doc/source/whatsnew/v2.1.0.rst b/doc/source/whatsnew/v2.1.0.rst index dc306471dbd3f..df3faa99ca970 100644 --- a/doc/source/whatsnew/v2.1.0.rst +++ b/doc/source/whatsnew/v2.1.0.rst @@ -395,6 +395,7 @@ Timedelta - Bug in :class:`Timedelta` with Numpy timedelta64 objects not properly raising ``ValueError`` (:issue:`52806`) - 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`) +- Bug in :meth:`Timedelta.__hash__`, raising an ``OutOfBoundsTimedelta`` on certain large values of second resolution (:issue:`54037`) - Timezones From 015c69332f195f0f74c2434b2559082c6057d1cf Mon Sep 17 00:00:00 2001 From: Harald Husum Date: Fri, 7 Jul 2023 17:55:01 +0200 Subject: [PATCH 5/5] Pre-commit after the fact --- doc/source/whatsnew/v2.1.0.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v2.1.0.rst b/doc/source/whatsnew/v2.1.0.rst index df3faa99ca970..97069e32a66e2 100644 --- a/doc/source/whatsnew/v2.1.0.rst +++ b/doc/source/whatsnew/v2.1.0.rst @@ -393,9 +393,9 @@ 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`) -- Bug in :meth:`Timedelta.__hash__`, raising an ``OutOfBoundsTimedelta`` on certain large values of second resolution (:issue:`54037`) - Timezones