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 1 commit
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
7 changes: 7 additions & 0 deletions pandas/core/util/hashing.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,13 @@ def hash_array(
if is_categorical_dtype(dtype):
vals = cast("Categorical", vals)
return _hash_categorical(vals, encoding, hash_key)

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you instead check that this is an extension array and use an else clause?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure

# i.e. ExtensionArray
vals, _ = vals._values_for_factorize()
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