Skip to content

Commit 98ecede

Browse files
committed
FIX hash of NDFrame raises TypeError
1 parent a7f37d4 commit 98ecede

File tree

7 files changed

+29
-1
lines changed

7 files changed

+29
-1
lines changed

RELEASE.rst

+1
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,7 @@ pandas 0.11.1
220220
- Groupby transform with item-by-item not upcasting correctly (GH3740_)
221221
- Incorrectly read a HDFStore multi-index Frame witha column specification (GH3748_)
222222
- ``read_html`` now correctly skips tests (GH3741_)
223+
- DataFrames/Panel raise Type error when trying to hash (GH3882_)
223224
- Fix incorrect arguments passed to concat that are not list-like (e.g. concat(df1,df2)) (GH3481_)
224225
- Correctly parse when passed the ``dtype=str`` (or other variable-len string dtypes) in ``read_csv`` (GH3795_)
225226

pandas/core/generic.py

+4
Original file line numberDiff line numberDiff line change
@@ -594,6 +594,10 @@ def axes(self):
594594
def __repr__(self):
595595
return 'NDFrame'
596596

597+
def __hash__(self):
598+
raise TypeError('{0!r} objects are mutable, thus they cannot be'
599+
' hashed'.format(self.__class__.__name__))
600+
597601
@property
598602
def values(self):
599603
return self._data.as_matrix()

pandas/core/series.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -528,7 +528,8 @@ def _can_hold_na(self):
528528
return not is_integer_dtype(self.dtype)
529529

530530
def __hash__(self):
531-
raise TypeError('unhashable type')
531+
raise TypeError('{0!r} objects are mutable, thus they cannot be'
532+
' hashed'.format(self.__class__.__name__))
532533

533534
_index = None
534535
index = lib.SeriesIndex()

pandas/tests/test_frame.py

+5
Original file line numberDiff line numberDiff line change
@@ -3109,6 +3109,11 @@ def test_constructor_for_list_with_dtypes(self):
31093109
expected.sort()
31103110
assert_series_equal(result, expected)
31113111

3112+
def test_not_hashable(self):
3113+
df = pd.DataFrame([1])
3114+
self.assertRaises(TypeError, hash, df)
3115+
self.assertRaises(TypeError, hash, self.empty)
3116+
31123117
def test_timedeltas(self):
31133118

31143119
df = DataFrame(dict(A = Series(date_range('2012-1-1', periods=3, freq='D')),

pandas/tests/test_panel.py

+6
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,12 @@ def test_cumsum(self):
4646
cumsum = self.panel.cumsum()
4747
assert_frame_equal(cumsum['ItemA'], self.panel['ItemA'].cumsum())
4848

49+
def not_hashable(self):
50+
c_empty = Panel()
51+
c = Panel(pd.Panel([[[1]]]))
52+
self.assertRaises(TypeError, hash, c_empty)
53+
self.assertRaises(TypeError, hash, c)
54+
4955

5056
class SafeForLongAndSparse(object):
5157
_multiprocess_can_split_ = True

pandas/tests/test_panel4d.py

+5
Original file line numberDiff line numberDiff line change
@@ -785,6 +785,11 @@ def test_reindex(self):
785785
major=self.panel4d.major_axis, copy=False)
786786
self.assert_(result is self.panel4d)
787787

788+
def test_not_hashable(self):
789+
p4D_empty = Panel4D()
790+
self.assertRaises(TypeError, hash, p4D_empty)
791+
self.assertRaises(TypeError, hash, self.panel4d)
792+
788793
def test_reindex_like(self):
789794
# reindex_like
790795
smaller = self.panel4d.reindex(labels=self.panel4d.labels[:-1],

pandas/tests/test_series.py

+6
Original file line numberDiff line numberDiff line change
@@ -579,6 +579,12 @@ def test_setindex(self):
579579
def test_array_finalize(self):
580580
pass
581581

582+
def test_not_hashable(self):
583+
s_empty = Series()
584+
s = Series([1])
585+
self.assertRaises(TypeError, hash, s_empty)
586+
self.assertRaises(TypeError, hash, s)
587+
582588
def test_fromValue(self):
583589

584590
nans = Series(np.NaN, index=self.ts.index)

0 commit comments

Comments
 (0)