Skip to content

ER: give a better error message for hashing indices #5019

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
Sep 28, 2013
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
3 changes: 2 additions & 1 deletion pandas/core/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -600,7 +600,7 @@ def __contains__(self, key):
return False

def __hash__(self):
return hash(self.view(np.ndarray))
raise TypeError("unhashable type: %r" % type(self).__name__)
Copy link
Contributor

Choose a reason for hiding this comment

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

is this actually used anywhere? or just provided for compat?

Copy link
Member Author

Choose a reason for hiding this comment

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

it raises the same TypeError as my change does, but my change shows the subclass name

Copy link
Member Author

Choose a reason for hiding this comment

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

so i'm thinking compat

Copy link
Member Author

Choose a reason for hiding this comment

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

hash raise test has been around since june 23 2011, incidentally that's my birthday


def __getitem__(self, key):
"""Override numpy.ndarray's __getitem__ method to work as desired"""
Expand Down Expand Up @@ -1852,6 +1852,7 @@ def equals(self, other):
# e.g. fails in numpy 1.6 with DatetimeIndex #1681
return False


class MultiIndex(Index):

"""
Expand Down
28 changes: 26 additions & 2 deletions pandas/tests/test_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
import numpy as np
from numpy.testing import assert_array_equal

from pandas.core.index import Index, Float64Index, Int64Index, MultiIndex, InvalidIndexError
from pandas.core.index import (Index, Float64Index, Int64Index, MultiIndex,
InvalidIndexError)
from pandas.core.frame import DataFrame
from pandas.core.series import Series
from pandas.util.testing import (assert_almost_equal, assertRaisesRegexp,
Expand Down Expand Up @@ -75,7 +76,10 @@ def test_set_name_methods(self):
self.assertEqual(ind.names, [name])

def test_hash_error(self):
self.assertRaises(TypeError, hash, self.strIndex)
with tm.assertRaisesRegexp(TypeError,
"unhashable type: %r" %
type(self.strIndex).__name__):
hash(self.strIndex)

def test_new_axis(self):
new_index = self.dateIndex[None, :]
Expand Down Expand Up @@ -661,6 +665,12 @@ def setUp(self):
self.mixed = Float64Index([1.5, 2, 3, 4, 5])
self.float = Float64Index(np.arange(5) * 2.5)

def test_hash_error(self):
with tm.assertRaisesRegexp(TypeError,
"unhashable type: %r" %
type(self.float).__name__):
hash(self.float)

def check_is_index(self, i):
self.assert_(isinstance(i, Index) and not isinstance(i, Float64Index))

Expand Down Expand Up @@ -736,6 +746,7 @@ def test_astype(self):
self.assert_(i.equals(result))
self.check_is_index(result)


class TestInt64Index(unittest.TestCase):
_multiprocess_can_split_ = True

Expand Down Expand Up @@ -779,6 +790,12 @@ def test_constructor_corner(self):
arr = np.array([1, '2', 3, '4'], dtype=object)
self.assertRaises(TypeError, Int64Index, arr)

def test_hash_error(self):
with tm.assertRaisesRegexp(TypeError,
"unhashable type: %r" %
type(self.index).__name__):
hash(self.index)

def test_copy(self):
i = Int64Index([], name='Foo')
i_copy = i.copy()
Expand Down Expand Up @@ -1155,6 +1172,12 @@ def setUp(self):
labels=[major_labels, minor_labels],
names=self.index_names)

def test_hash_error(self):
with tm.assertRaisesRegexp(TypeError,
"unhashable type: %r" %
type(self.index).__name__):
hash(self.index)

def test_set_names_and_rename(self):
# so long as these are synonyms, we don't need to test set_names
self.assert_(self.index.rename == self.index.set_names)
Expand Down Expand Up @@ -2231,6 +2254,7 @@ def test_get_combined_index():
result = _get_combined_index([])
assert(result.equals(Index([])))


if __name__ == '__main__':
nose.runmodule(argv=[__file__, '-vvs', '-x', '--pdb', '--pdb-failure'],
exit=False)
7 changes: 7 additions & 0 deletions pandas/tseries/tests/test_period.py
Original file line number Diff line number Diff line change
Expand Up @@ -1057,6 +1057,13 @@ class TestPeriodIndex(TestCase):
def setUp(self):
pass

def test_hash_error(self):
index = period_range('20010101', periods=10)
with tm.assertRaisesRegexp(TypeError,
"unhashable type: %r" %
type(index).__name__):
hash(index)

def test_make_time_series(self):
index = PeriodIndex(freq='A', start='1/1/2001', end='12/1/2009')
series = Series(1, index=index)
Expand Down
7 changes: 7 additions & 0 deletions pandas/tseries/tests/test_timeseries.py
Original file line number Diff line number Diff line change
Expand Up @@ -1716,6 +1716,13 @@ def _simple_ts(start, end, freq='D'):
class TestDatetimeIndex(unittest.TestCase):
_multiprocess_can_split_ = True

def test_hash_error(self):
index = date_range('20010101', periods=10)
with tm.assertRaisesRegexp(TypeError,
"unhashable type: %r" %
type(index).__name__):
hash(index)

def test_stringified_slice_with_tz(self):
#GH2658
import datetime
Expand Down