Skip to content

BUG: hash_array TypeError instead of AttributeError on Index GH#42003 #45301

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 4 commits into from
Jan 16, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
12 changes: 10 additions & 2 deletions pandas/core/util/hashing.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
)
from pandas.core.dtypes.generic import (
ABCDataFrame,
ABCExtensionArray,
ABCIndex,
ABCMultiIndex,
ABCSeries,
Expand Down Expand Up @@ -286,10 +287,17 @@ def hash_array(
if is_categorical_dtype(dtype):
vals = cast("Categorical", vals)
return _hash_categorical(vals, encoding, hash_key)
elif not isinstance(vals, np.ndarray):
# i.e. ExtensionArray

elif isinstance(vals, ABCExtensionArray):
vals, _ = vals._values_for_factorize()

elif not isinstance(vals, np.ndarray):
# GH#42003
raise TypeError(
"hash_array requires np.ndarray or ExtensionArray, not "
f"{type(vals).__name__}. Use hash_pandas_object instead."
)

return _hash_ndarray(vals, encoding, hash_key, categorize)


Expand Down
9 changes: 9 additions & 0 deletions pandas/tests/util/test_hashing.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,15 @@ def test_hash_array_errors(val):
hash_array(val)


def test_hash_array_index_exception():
# GH42003 TypeError instead of AttributeError
obj = pd.DatetimeIndex(["2018-10-28 01:20:00"], tz="Europe/Berlin")

msg = "Use hash_pandas_object instead"
with pytest.raises(TypeError, match=msg):
hash_array(obj)


def test_hash_tuples():
tuples = [(1, "one"), (1, "two"), (2, "one")]
result = hash_tuples(tuples)
Expand Down