File tree Expand file tree Collapse file tree 2 files changed +24
-1
lines changed Expand file tree Collapse file tree 2 files changed +24
-1
lines changed Original file line number Diff line number Diff line change @@ -15,13 +15,17 @@ from pandas._libs.tslibs.np_datetime cimport (
15
15
from pandas._libs.tslibs.nattype cimport (
16
16
checknull_with_nat, c_NaT as NaT, is_null_datetimelike)
17
17
18
+ from pandas.compat import is_platform_32bit
19
+
18
20
19
21
cdef:
20
22
float64_t INF = < float64_t> np.inf
21
23
float64_t NEGINF = - INF
22
24
23
25
int64_t NPY_NAT = util.get_nat()
24
26
27
+ bint is_32bit = is_platform_32bit()
28
+
25
29
26
30
cpdef bint checknull(object val):
27
31
"""
@@ -345,7 +349,9 @@ class NAType(C_NAType):
345
349
raise TypeError (" boolean value of NA is ambiguous" )
346
350
347
351
def __hash__ (self ):
348
- return id (self )
352
+ # GH 30013: Ensure hash is large enough to avoid hash collisions with integers
353
+ exponent = 31 if is_32bit else 61
354
+ return 2 ** exponent - 1
349
355
350
356
# Binary arithmetic and comparison ops -> propagate
351
357
Original file line number Diff line number Diff line change @@ -175,3 +175,20 @@ def test_series_isna():
175
175
s = pd .Series ([1 , NA ], dtype = object )
176
176
expected = pd .Series ([False , True ])
177
177
tm .assert_series_equal (s .isna (), expected )
178
+
179
+
180
+ def test_integer_hash_collision_dict ():
181
+ # GH 30013
182
+ result = {NA : "foo" , hash (NA ): "bar" }
183
+
184
+ assert result [NA ] == "foo"
185
+ assert result [hash (NA )] == "bar"
186
+
187
+
188
+ def test_integer_hash_collision_set ():
189
+ # GH 30013
190
+ result = {NA , hash (NA )}
191
+
192
+ assert len (result ) == 2
193
+ assert NA in result
194
+ assert hash (NA ) in result
You can’t perform that action at this time.
0 commit comments