Skip to content

FIX hash of DataFrame raises Typerror #3884

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 2 commits into from
Jun 13, 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
1 change: 1 addition & 0 deletions RELEASE.rst
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ pandas 0.11.1
- Groupby transform with item-by-item not upcasting correctly (GH3740_)
- Incorrectly read a HDFStore multi-index Frame witha column specification (GH3748_)
- ``read_html`` now correctly skips tests (GH3741_)
- PandasObjects raise TypeError when trying to hash (GH3882_)
- Fix incorrect arguments passed to concat that are not list-like (e.g. concat(df1,df2)) (GH3481_)
- Correctly parse when passed the ``dtype=str`` (or other variable-len string dtypes) in ``read_csv`` (GH3795_)

Expand Down
5 changes: 5 additions & 0 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ def save(self, path):
def load(cls, path):
return com.load(path)

def __hash__(self):
raise TypeError('{0!r} objects are mutable, thus they cannot be'
' hashed'.format(self.__class__.__name__))


#----------------------------------------------------------------------
# Axis name business

Expand Down
3 changes: 0 additions & 3 deletions pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -527,9 +527,6 @@ def _constructor(self):
def _can_hold_na(self):
return not is_integer_dtype(self.dtype)

def __hash__(self):
raise TypeError('unhashable type')

_index = None
index = lib.SeriesIndex()

Expand Down
5 changes: 5 additions & 0 deletions pandas/tests/test_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -3109,6 +3109,11 @@ def test_constructor_for_list_with_dtypes(self):
expected.sort()
assert_series_equal(result, expected)

def test_not_hashable(self):
df = pd.DataFrame([1])
self.assertRaises(TypeError, hash, df)
self.assertRaises(TypeError, hash, self.empty)

def test_timedeltas(self):

df = DataFrame(dict(A = Series(date_range('2012-1-1', periods=3, freq='D')),
Expand Down
6 changes: 6 additions & 0 deletions pandas/tests/test_panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ def test_cumsum(self):
cumsum = self.panel.cumsum()
assert_frame_equal(cumsum['ItemA'], self.panel['ItemA'].cumsum())

def not_hashable(self):
c_empty = Panel()
c = Panel(pd.Panel([[[1]]]))
self.assertRaises(TypeError, hash, c_empty)
self.assertRaises(TypeError, hash, c)


class SafeForLongAndSparse(object):
_multiprocess_can_split_ = True
Expand Down
5 changes: 5 additions & 0 deletions pandas/tests/test_panel4d.py
Original file line number Diff line number Diff line change
Expand Up @@ -785,6 +785,11 @@ def test_reindex(self):
major=self.panel4d.major_axis, copy=False)
self.assert_(result is self.panel4d)

def test_not_hashable(self):
p4D_empty = Panel4D()
self.assertRaises(TypeError, hash, p4D_empty)
self.assertRaises(TypeError, hash, self.panel4d)

def test_reindex_like(self):
# reindex_like
smaller = self.panel4d.reindex(labels=self.panel4d.labels[:-1],
Expand Down
6 changes: 6 additions & 0 deletions pandas/tests/test_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -579,6 +579,12 @@ def test_setindex(self):
def test_array_finalize(self):
pass

def test_not_hashable(self):
s_empty = Series()
s = Series([1])
self.assertRaises(TypeError, hash, s_empty)
self.assertRaises(TypeError, hash, s)

def test_fromValue(self):

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