Skip to content

Commit 860013f

Browse files
committed
ENH: Adding default implementation to ExtensionArray equals() and tests.
pandas-devGH-27081
1 parent a3e7b7f commit 860013f

File tree

4 files changed

+16
-24
lines changed

4 files changed

+16
-24
lines changed

pandas/core/arrays/base.py

+13-8
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,6 @@ class ExtensionArray:
105105
* _from_sequence
106106
* _from_factorized
107107
* __getitem__
108-
* __eq__
109108
* __len__
110109
* dtype
111110
* nbytes
@@ -358,24 +357,28 @@ def __eq__(self, other: Any) -> bool:
358357
359358
Parameters
360359
----------
361-
other: ExtensionArray
362-
The array to compare to this array.
360+
other: Any
361+
The object to compare to this array.
363362
364363
Returns
365364
-------
366365
bool
367366
"""
368367

369-
raise AbstractMethodError(self)
368+
return (
369+
type(self) == type(other)
370+
and (self.isna() == other.isna()).all()
371+
and np.all(np.array(self) == np.array(other))
372+
)
370373

371374
def __ne__(self, other: Any) -> bool:
372375
"""
373376
Whether the two arrays are not equivalent.
374377
375378
Parameters
376379
----------
377-
other: ExtensionArray
378-
The array to compare to this array.
380+
other: Any
381+
The object to compare to this array.
379382
380383
Returns
381384
-------
@@ -706,8 +709,10 @@ def equals(self, other: ABCExtensionArray) -> bool:
706709
Whether the arrays are equivalent.
707710
708711
"""
709-
return isinstance(other, self.__class__) and (
710-
((self == other) | (self.isna() == other.isna())).all()
712+
return (
713+
type(self) == type(other)
714+
and (((self == other) | (self.isna() == other.isna())).all())
715+
and len(self) == len(other)
711716
)
712717

713718
def _values_for_factorize(self) -> Tuple[np.ndarray, Any]:

pandas/core/arrays/integer.py

-9
Original file line numberDiff line numberDiff line change
@@ -376,15 +376,6 @@ def __getitem__(self, item):
376376

377377
return type(self)(self._data[item], self._mask[item])
378378

379-
def __eq__(self, other):
380-
return (
381-
isinstance(other, IntegerArray)
382-
and hasattr(other, "_data")
383-
and self._data == other._data
384-
and hasattr(other, "_mask")
385-
and self._mask == other._mask
386-
)
387-
388379
def _coerce_to_ndarray(self, dtype=None, na_value=lib._no_default):
389380
"""
390381
coerce to an ndarary of object dtype

pandas/tests/extension/base/methods.py

+3
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,7 @@ def test_repeat_raises(self, data, repeats, kwargs, error, msg, use_numpy):
362362
def test_equals(self, data, na_value):
363363
cls = type(data)
364364
ser = pd.Series(cls._from_sequence(data, dtype=data.dtype))
365+
smaller_ser = pd.Series(cls._from_sequence(data[:5], dtype=data.dtype))
365366
na_ser = pd.Series(cls._from_sequence([na_value], dtype=data.dtype))
366367

367368
assert data.equals(data)
@@ -371,5 +372,7 @@ def test_equals(self, data, na_value):
371372
assert not data.equals(na_value)
372373
assert not na_ser.equals(ser)
373374
assert not ser.equals(na_ser)
375+
assert not ser.equals(smaller_ser)
376+
assert not ser.equals(np.asarray(data))
374377
assert not ser.equals(0)
375378
assert not na_ser.equals(0)

pandas/tests/extension/json/array.py

-7
Original file line numberDiff line numberDiff line change
@@ -110,13 +110,6 @@ def __setitem__(self, key, value):
110110
assert isinstance(v, self.dtype.type)
111111
self.data[k] = v
112112

113-
def __eq__(self, other):
114-
return (
115-
isinstance(other, JSONArray)
116-
and hasattr(other, "data")
117-
and self.data == other.data
118-
)
119-
120113
def __len__(self) -> int:
121114
return len(self.data)
122115

0 commit comments

Comments
 (0)