File tree 1 file changed +26
-0
lines changed
pandas/tests/scalar/timedelta
1 file changed +26
-0
lines changed Original file line number Diff line number Diff line change 1
1
""" test the scalar Timedelta """
2
2
from datetime import timedelta
3
+ import sys
4
+ from typing import Hashable
3
5
4
6
from hypothesis import (
5
7
given ,
@@ -918,6 +920,30 @@ def test_timedelta_hash_equality(self):
918
920
ns_td = Timedelta (1 , "ns" )
919
921
assert hash (ns_td ) != hash (ns_td .to_pytimedelta ())
920
922
923
+ @pytest .mark .xfail (
924
+ reason = "pd.Timedelta violates the Python hash invariant (GH#44504)." ,
925
+ raises = AssertionError ,
926
+ strict = True ,
927
+ )
928
+ @given (st .integers (min_value = (- sys .maxsize - 1 ) / 500 , max_value = sys .maxsize / 500 ))
929
+ def test_hash_equality_invariance (self , half_microseconds : int ) -> None :
930
+ # GH#44504
931
+
932
+ def _upholds_hash_equality_invariance (v1 : Hashable , v2 : Hashable , / ) -> bool :
933
+ if v1 != v2 :
934
+ return True
935
+
936
+ # See: https://docs.python.org/3/glossary.html#term-hashable
937
+ # Hashable objects which compare equal must have the same hash value.
938
+ return hash (v1 ) == hash (v2 )
939
+
940
+ nanoseconds = half_microseconds * 500
941
+
942
+ pandas_timedelta = Timedelta (nanoseconds )
943
+ numpy_timedelta = np .timedelta64 (nanoseconds )
944
+
945
+ assert _upholds_hash_equality_invariance (pandas_timedelta , numpy_timedelta )
946
+
921
947
def test_implementation_limits (self ):
922
948
min_td = Timedelta (Timedelta .min )
923
949
max_td = Timedelta (Timedelta .max )
You can’t perform that action at this time.
0 commit comments