Skip to content

Commit 998e742

Browse files
jbrockmendeljreback
authored andcommitted
BUG: EAs should not be hashable (#30908)
1 parent 06e416d commit 998e742

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

pandas/core/arrays/base.py

+6
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,9 @@ class ExtensionArray:
176176
types present.
177177
178178
See :ref:`extending.extension.ufunc` for more.
179+
180+
By default, ExtensionArrays are not hashable. Immutable subclasses may
181+
override this behavior.
179182
"""
180183

181184
# '_typ' is for pandas.core.dtypes.generic.ABCExtensionArray.
@@ -1073,6 +1076,9 @@ def _reduce(self, name, skipna=True, **kwargs):
10731076
"""
10741077
raise TypeError(f"cannot perform {name} with type {self.dtype}")
10751078

1079+
def __hash__(self):
1080+
raise TypeError(f"unhashable type: {repr(type(self).__name__)}")
1081+
10761082

10771083
class ExtensionOpsMixin:
10781084
"""

pandas/tests/extension/base/methods.py

+5
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,11 @@ def test_shift_fill_value(self, data):
261261
expected = data.take([2, 3, 0, 0])
262262
self.assert_extension_array_equal(result, expected)
263263

264+
def test_not_hashable(self, data):
265+
# We are in general mutable, so not hashable
266+
with pytest.raises(TypeError, match="unhashable type"):
267+
hash(data)
268+
264269
def test_hash_pandas_object_works(self, data, as_frame):
265270
# https://github.com/pandas-dev/pandas/issues/23066
266271
data = pd.Series(data)

0 commit comments

Comments
 (0)