Skip to content

Commit dd6e31a

Browse files
REGR: Fixed hash_key=None for object values (#30900)
* REGR: Fixed hash_key=None for object values Closes #30887
1 parent 62d16ab commit dd6e31a

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

pandas/core/util/hashing.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
data hash pandas / numpy objects
33
"""
44
import itertools
5+
from typing import Optional
56

67
import numpy as np
78

@@ -58,7 +59,7 @@ def hash_pandas_object(
5859
obj,
5960
index: bool = True,
6061
encoding: str = "utf8",
61-
hash_key: str = _default_hash_key,
62+
hash_key: Optional[str] = _default_hash_key,
6263
categorize: bool = True,
6364
):
6465
"""
@@ -82,6 +83,9 @@ def hash_pandas_object(
8283
"""
8384
from pandas import Series
8485

86+
if hash_key is None:
87+
hash_key = _default_hash_key
88+
8589
if isinstance(obj, ABCMultiIndex):
8690
return Series(hash_tuples(obj, encoding, hash_key), dtype="uint64", copy=False)
8791

pandas/tests/util/test_hashing.py

+7
Original file line numberDiff line numberDiff line change
@@ -374,3 +374,10 @@ def test_hash_with_tuple():
374374
df3 = pd.DataFrame({"data": [tuple([1, []]), tuple([2, {}])]})
375375
with pytest.raises(TypeError, match="unhashable type: 'list'"):
376376
hash_pandas_object(df3)
377+
378+
379+
def test_hash_object_none_key():
380+
# https://github.com/pandas-dev/pandas/issues/30887
381+
result = pd.util.hash_pandas_object(pd.Series(["a", "b"]), hash_key=None)
382+
expected = pd.Series([4578374827886788867, 17338122309987883691], dtype="uint64")
383+
tm.assert_series_equal(result, expected)

0 commit comments

Comments
 (0)