Skip to content

Commit b67268a

Browse files
committed
ER: give a better error message for hashing indices
1 parent d3b48a8 commit b67268a

File tree

4 files changed

+42
-3
lines changed

4 files changed

+42
-3
lines changed

pandas/core/index.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -600,7 +600,7 @@ def __contains__(self, key):
600600
return False
601601

602602
def __hash__(self):
603-
return hash(self.view(np.ndarray))
603+
raise TypeError("unhashable type: %r" % type(self).__name__)
604604

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

1855+
18551856
class MultiIndex(Index):
18561857

18571858
"""

pandas/tests/test_index.py

+26-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
import numpy as np
1313
from numpy.testing import assert_array_equal
1414

15-
from pandas.core.index import Index, Float64Index, Int64Index, MultiIndex, InvalidIndexError
15+
from pandas.core.index import (Index, Float64Index, Int64Index, MultiIndex,
16+
InvalidIndexError)
1617
from pandas.core.frame import DataFrame
1718
from pandas.core.series import Series
1819
from pandas.util.testing import (assert_almost_equal, assertRaisesRegexp,
@@ -75,7 +76,10 @@ def test_set_name_methods(self):
7576
self.assertEqual(ind.names, [name])
7677

7778
def test_hash_error(self):
78-
self.assertRaises(TypeError, hash, self.strIndex)
79+
with tm.assertRaisesRegexp(TypeError,
80+
"unhashable type: %r" %
81+
type(self.strIndex).__name__):
82+
hash(self.strIndex)
7983

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

668+
def test_hash_error(self):
669+
with tm.assertRaisesRegexp(TypeError,
670+
"unhashable type: %r" %
671+
type(self.float).__name__):
672+
hash(self.float)
673+
664674
def check_is_index(self, i):
665675
self.assert_(isinstance(i, Index) and not isinstance(i, Float64Index))
666676

@@ -736,6 +746,7 @@ def test_astype(self):
736746
self.assert_(i.equals(result))
737747
self.check_is_index(result)
738748

749+
739750
class TestInt64Index(unittest.TestCase):
740751
_multiprocess_can_split_ = True
741752

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

793+
def test_hash_error(self):
794+
with tm.assertRaisesRegexp(TypeError,
795+
"unhashable type: %r" %
796+
type(self.index).__name__):
797+
hash(self.index)
798+
782799
def test_copy(self):
783800
i = Int64Index([], name='Foo')
784801
i_copy = i.copy()
@@ -1155,6 +1172,12 @@ def setUp(self):
11551172
labels=[major_labels, minor_labels],
11561173
names=self.index_names)
11571174

1175+
def test_hash_error(self):
1176+
with tm.assertRaisesRegexp(TypeError,
1177+
"unhashable type: %r" %
1178+
type(self.index).__name__):
1179+
hash(self.index)
1180+
11581181
def test_set_names_and_rename(self):
11591182
# so long as these are synonyms, we don't need to test set_names
11601183
self.assert_(self.index.rename == self.index.set_names)
@@ -2231,6 +2254,7 @@ def test_get_combined_index():
22312254
result = _get_combined_index([])
22322255
assert(result.equals(Index([])))
22332256

2257+
22342258
if __name__ == '__main__':
22352259
nose.runmodule(argv=[__file__, '-vvs', '-x', '--pdb', '--pdb-failure'],
22362260
exit=False)

pandas/tseries/tests/test_period.py

+7
Original file line numberDiff line numberDiff line change
@@ -1057,6 +1057,13 @@ class TestPeriodIndex(TestCase):
10571057
def setUp(self):
10581058
pass
10591059

1060+
def test_hash_error(self):
1061+
index = period_range('20010101', periods=10)
1062+
with tm.assertRaisesRegexp(TypeError,
1063+
"unhashable type: %r" %
1064+
type(index).__name__):
1065+
hash(index)
1066+
10601067
def test_make_time_series(self):
10611068
index = PeriodIndex(freq='A', start='1/1/2001', end='12/1/2009')
10621069
series = Series(1, index=index)

pandas/tseries/tests/test_timeseries.py

+7
Original file line numberDiff line numberDiff line change
@@ -1716,6 +1716,13 @@ def _simple_ts(start, end, freq='D'):
17161716
class TestDatetimeIndex(unittest.TestCase):
17171717
_multiprocess_can_split_ = True
17181718

1719+
def test_hash_error(self):
1720+
index = date_range('20010101', periods=10)
1721+
with tm.assertRaisesRegexp(TypeError,
1722+
"unhashable type: %r" %
1723+
type(index).__name__):
1724+
hash(index)
1725+
17191726
def test_stringified_slice_with_tz(self):
17201727
#GH2658
17211728
import datetime

0 commit comments

Comments
 (0)