From f92a39d194fca1b52627e8652a17cc983c8956c1 Mon Sep 17 00:00:00 2001 From: Brock Mendel Date: Fri, 10 Jan 2020 17:44:00 -0800 Subject: [PATCH 1/2] BUG: EAs should not be hashable --- pandas/core/arrays/base.py | 3 +++ pandas/tests/extension/base/methods.py | 5 +++++ 2 files changed, 8 insertions(+) diff --git a/pandas/core/arrays/base.py b/pandas/core/arrays/base.py index 9723343ea7af5..bd5b0ef390804 100644 --- a/pandas/core/arrays/base.py +++ b/pandas/core/arrays/base.py @@ -1073,6 +1073,9 @@ def _reduce(self, name, skipna=True, **kwargs): """ raise TypeError(f"cannot perform {name} with type {self.dtype}") + def __hash__(self): + raise TypeError(f"unhashable type: {repr(type(self).__name__)}") + class ExtensionOpsMixin: """ diff --git a/pandas/tests/extension/base/methods.py b/pandas/tests/extension/base/methods.py index 1e427c6319cab..6b75176ebd35b 100644 --- a/pandas/tests/extension/base/methods.py +++ b/pandas/tests/extension/base/methods.py @@ -261,6 +261,11 @@ def test_shift_fill_value(self, data): expected = data.take([2, 3, 0, 0]) self.assert_extension_array_equal(result, expected) + def test_not_hashable(self, data): + # We are in general mutable, so not hashable + with pytest.raises(TypeError, match="unhashable type"): + hash(data) + def test_hash_pandas_object_works(self, data, as_frame): # https://github.com/pandas-dev/pandas/issues/23066 data = pd.Series(data) From 3851d9095d193d6e553c341a070c175da0a69870 Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Sun, 19 Jan 2020 13:46:05 -0800 Subject: [PATCH 2/2] docstring --- pandas/core/arrays/base.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/pandas/core/arrays/base.py b/pandas/core/arrays/base.py index bd5b0ef390804..c3c91cea43f6b 100644 --- a/pandas/core/arrays/base.py +++ b/pandas/core/arrays/base.py @@ -176,6 +176,9 @@ class ExtensionArray: types present. See :ref:`extending.extension.ufunc` for more. + + By default, ExtensionArrays are not hashable. Immutable subclasses may + override this behavior. """ # '_typ' is for pandas.core.dtypes.generic.ABCExtensionArray.