Skip to content

Commit 5acb14b

Browse files
authored
BUG: hash_array TypeError instead of AttributeError on Index GH#42003 (#45301)
1 parent 51675d0 commit 5acb14b

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

pandas/core/util/hashing.py

+10-2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
)
2525
from pandas.core.dtypes.generic import (
2626
ABCDataFrame,
27+
ABCExtensionArray,
2728
ABCIndex,
2829
ABCMultiIndex,
2930
ABCSeries,
@@ -286,10 +287,17 @@ def hash_array(
286287
if is_categorical_dtype(dtype):
287288
vals = cast("Categorical", vals)
288289
return _hash_categorical(vals, encoding, hash_key)
289-
elif not isinstance(vals, np.ndarray):
290-
# i.e. ExtensionArray
290+
291+
elif isinstance(vals, ABCExtensionArray):
291292
vals, _ = vals._values_for_factorize()
292293

294+
elif not isinstance(vals, np.ndarray):
295+
# GH#42003
296+
raise TypeError(
297+
"hash_array requires np.ndarray or ExtensionArray, not "
298+
f"{type(vals).__name__}. Use hash_pandas_object instead."
299+
)
300+
293301
return _hash_ndarray(vals, encoding, hash_key, categorize)
294302

295303

pandas/tests/util/test_hashing.py

+9
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,15 @@ def test_hash_array_errors(val):
7171
hash_array(val)
7272

7373

74+
def test_hash_array_index_exception():
75+
# GH42003 TypeError instead of AttributeError
76+
obj = pd.DatetimeIndex(["2018-10-28 01:20:00"], tz="Europe/Berlin")
77+
78+
msg = "Use hash_pandas_object instead"
79+
with pytest.raises(TypeError, match=msg):
80+
hash_array(obj)
81+
82+
7483
def test_hash_tuples():
7584
tuples = [(1, "one"), (1, "two"), (2, "one")]
7685
result = hash_tuples(tuples)

0 commit comments

Comments
 (0)