Skip to content

BUG: fix common.is_hashable for NumPy scalars on Python 3 #9473

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 1 commit into from
Feb 16, 2015
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: 6 additions & 6 deletions pandas/core/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -2537,13 +2537,13 @@ def is_hashable(arg):
>>> is_hashable(a)
False
"""
# don't consider anything not collections.Hashable, so as not to broaden
# the definition of hashable beyond that. For example, old-style classes
# are not collections.Hashable but they won't fail hash().
if not isinstance(arg, collections.Hashable):
return False
# unfortunately, we can't use isinstance(arg, collections.Hashable), which
# can be faster than calling hash, because numpy scalars on Python 3 fail
# this test

# reconsider this decision once this numpy bug is fixed:
# https://github.com/numpy/numpy/issues/5562

# narrow the definition of hashable if hash(arg) fails in practice
try:
hash(arg)
except TypeError:
Expand Down
7 changes: 2 additions & 5 deletions pandas/tests/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@ def __hash__(self):
raise TypeError("Not hashable")

hashable = (
1, 'a', tuple(), (1,), HashableClass(),
1, 3.14, np.float64(3.14), 'a', tuple(), (1,), HashableClass(),
)
not_hashable = (
[], UnhashableClass1(),
Expand All @@ -434,13 +434,10 @@ def __hash__(self):
)

for i in hashable:
assert isinstance(i, collections.Hashable)
assert com.is_hashable(i)
for i in not_hashable:
assert not isinstance(i, collections.Hashable)
assert not com.is_hashable(i)
for i in abc_hashable_not_really_hashable:
assert isinstance(i, collections.Hashable)
assert not com.is_hashable(i)

# numpy.array is no longer collections.Hashable as of
Expand All @@ -455,7 +452,7 @@ class OldStyleClass():
pass
c = OldStyleClass()
assert not isinstance(c, collections.Hashable)
assert not com.is_hashable(c)
assert com.is_hashable(c)
hash(c) # this will not raise


Expand Down