diff --git a/pandas/core/index.py b/pandas/core/index.py index d488a29182a18..ffa1d7cfa0c3b 100644 --- a/pandas/core/index.py +++ b/pandas/core/index.py @@ -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__) def __getitem__(self, key): """Override numpy.ndarray's __getitem__ method to work as desired""" @@ -1852,6 +1852,7 @@ def equals(self, other): # e.g. fails in numpy 1.6 with DatetimeIndex #1681 return False + class MultiIndex(Index): """ diff --git a/pandas/tests/test_index.py b/pandas/tests/test_index.py index 857836fa698ce..87772b5a86326 100644 --- a/pandas/tests/test_index.py +++ b/pandas/tests/test_index.py @@ -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, @@ -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, :] @@ -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)) @@ -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 @@ -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() @@ -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) @@ -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) diff --git a/pandas/tseries/tests/test_period.py b/pandas/tseries/tests/test_period.py index 96e96607ad9de..173ebeb199b3b 100644 --- a/pandas/tseries/tests/test_period.py +++ b/pandas/tseries/tests/test_period.py @@ -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) diff --git a/pandas/tseries/tests/test_timeseries.py b/pandas/tseries/tests/test_timeseries.py index 0e5e3d1922ec4..d44ae94bdb718 100644 --- a/pandas/tseries/tests/test_timeseries.py +++ b/pandas/tseries/tests/test_timeseries.py @@ -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