Skip to content

Commit 0a720b6

Browse files
authored
TST: Add test for Timedelta hash invariance (#54035)
* Add test for timedelta hash equality inariance * Ensure int bounds * PR feedback
1 parent be09d66 commit 0a720b6

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

pandas/tests/scalar/timedelta/test_timedelta.py

+25
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
""" test the scalar Timedelta """
22
from datetime import timedelta
3+
import sys
34

45
from hypothesis import (
56
given,
@@ -918,6 +919,30 @@ def test_timedelta_hash_equality(self):
918919
ns_td = Timedelta(1, "ns")
919920
assert hash(ns_td) != hash(ns_td.to_pytimedelta())
920921

922+
@pytest.mark.xfail(
923+
reason="pd.Timedelta violates the Python hash invariant (GH#44504).",
924+
raises=AssertionError,
925+
)
926+
@given(
927+
st.integers(
928+
min_value=(-sys.maxsize - 1) // 500,
929+
max_value=sys.maxsize // 500,
930+
)
931+
)
932+
def test_hash_equality_invariance(self, half_microseconds: int) -> None:
933+
# GH#44504
934+
935+
nanoseconds = half_microseconds * 500
936+
937+
pandas_timedelta = Timedelta(nanoseconds)
938+
numpy_timedelta = np.timedelta64(nanoseconds)
939+
940+
# See: https://docs.python.org/3/glossary.html#term-hashable
941+
# Hashable objects which compare equal must have the same hash value.
942+
assert pandas_timedelta != numpy_timedelta or hash(pandas_timedelta) == hash(
943+
numpy_timedelta
944+
)
945+
921946
def test_implementation_limits(self):
922947
min_td = Timedelta(Timedelta.min)
923948
max_td = Timedelta(Timedelta.max)

0 commit comments

Comments
 (0)