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 2 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
31 changes: 31 additions & 0 deletions pandas/tests/scalar/timedelta/test_timedelta.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
""" test the scalar Timedelta """
from datetime import timedelta
import sys
from typing import Hashable

from hypothesis import (
given,
Expand Down Expand Up @@ -918,6 +920,35 @@ 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,
strict=True,
)
@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

def _upholds_hash_equality_invariance(v1: Hashable, v2: Hashable, /) -> bool:
if v1 != v2:
return True

# See: https://docs.python.org/3/glossary.html#term-hashable
# Hashable objects which compare equal must have the same hash value.
return hash(v1) == hash(v2)

nanoseconds = half_microseconds * 500

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

assert _upholds_hash_equality_invariance(pandas_timedelta, numpy_timedelta)

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