Skip to content

Commit 7327d69

Browse files
committed
Merge pull request #3884 from hayd/df_unhashable
FIX hash of DataFrame raises Typerror
2 parents 333ead6 + 6701ed3 commit 7327d69

File tree

7 files changed

+28
-3
lines changed

7 files changed

+28
-3
lines changed

RELEASE.rst

+1
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,7 @@ pandas 0.11.1
225225
- Groupby transform with item-by-item not upcasting correctly (GH3740_)
226226
- Incorrectly read a HDFStore multi-index Frame witha column specification (GH3748_)
227227
- ``read_html`` now correctly skips tests (GH3741_)
228+
- PandasObjects raise TypeError when trying to hash (GH3882_)
228229
- Fix incorrect arguments passed to concat that are not list-like (e.g. concat(df1,df2)) (GH3481_)
229230
- Correctly parse when passed the ``dtype=str`` (or other variable-len string dtypes) in ``read_csv`` (GH3795_)
230231
- ``DataFrame.itertuples()`` now works with frames with duplicate column

pandas/core/generic.py

+5
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ def save(self, path):
3131
def load(cls, path):
3232
return com.load(path)
3333

34+
def __hash__(self):
35+
raise TypeError('{0!r} objects are mutable, thus they cannot be'
36+
' hashed'.format(self.__class__.__name__))
37+
38+
3439
#----------------------------------------------------------------------
3540
# Axis name business
3641

pandas/core/series.py

-3
Original file line numberDiff line numberDiff line change
@@ -527,9 +527,6 @@ def _constructor(self):
527527
def _can_hold_na(self):
528528
return not is_integer_dtype(self.dtype)
529529

530-
def __hash__(self):
531-
raise TypeError('unhashable type')
532-
533530
_index = None
534531
index = lib.SeriesIndex()
535532

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)