Skip to content

TST: Add test for Timedelta hash invariance #54035

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 3 commits into from
Jul 12, 2023
Merged
Changes from all commits
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
25 changes: 25 additions & 0 deletions pandas/tests/scalar/timedelta/test_timedelta.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
""" test the scalar Timedelta """
from datetime import timedelta
import sys

from hypothesis import (
given,
Expand Down Expand Up @@ -918,6 +919,30 @@ def test_timedelta_hash_equality(self):
ns_td = Timedelta(1, "ns")
assert hash(ns_td) != hash(ns_td.to_pytimedelta())

@pytest.mark.xfail(
reason="pd.Timedelta violates the Python hash invariant (GH#44504).",
raises=AssertionError,
)
@given(
st.integers(
min_value=(-sys.maxsize - 1) // 500,
max_value=sys.maxsize // 500,
)
)
def test_hash_equality_invariance(self, half_microseconds: int) -> None:
# GH#44504

nanoseconds = half_microseconds * 500

pandas_timedelta = Timedelta(nanoseconds)
numpy_timedelta = np.timedelta64(nanoseconds)

# See: https://docs.python.org/3/glossary.html#term-hashable
# Hashable objects which compare equal must have the same hash value.
assert pandas_timedelta != numpy_timedelta or hash(pandas_timedelta) == hash(
numpy_timedelta
)

def test_implementation_limits(self):
min_td = Timedelta(Timedelta.min)
max_td = Timedelta(Timedelta.max)
Expand Down