Skip to content

Commit 3b2d07a

Browse files
jorisvandenbosschefangchenli
authored andcommitted
BUG/API: other object type check in Series/DataFrame.equals (pandas-dev#34402)
1 parent 7ebcac5 commit 3b2d07a

File tree

4 files changed

+19
-1
lines changed

4 files changed

+19
-1
lines changed

doc/source/whatsnew/v1.1.0.rst

+2
Original file line numberDiff line numberDiff line change
@@ -1200,6 +1200,8 @@ Other
12001200
- Bug in :meth:`DataFrame.__dir__` caused a segfault when using unicode surrogates in a column name (:issue:`25509`)
12011201
- Bug in :meth:`DataFrame.plot.scatter` caused an error when plotting variable marker sizes (:issue:`32904`)
12021202
- :class:`IntegerArray` now implements the ``sum`` operation (:issue:`33172`)
1203+
- Bug in :meth:`DataFrame.equals` and :meth:`Series.equals` in allowing subclasses
1204+
to be equal (:issue:`34402`).
12031205
- Bug in :class:`Tick` comparisons raising ``TypeError`` when comparing against timedelta-like objects (:issue:`34088`)
12041206
- Bug in :class:`Tick` multiplication raising ``TypeError`` when multiplying by a float (:issue:`34486`)
12051207
- Passing a `set` as `names` argument to :func:`pandas.read_csv`, :func:`pandas.read_table`, or :func:`pandas.read_fwf` will raise ``ValueError: Names should be an ordered collection.`` (:issue:`34946`)

pandas/core/generic.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1278,7 +1278,7 @@ def equals(self, other):
12781278
>>> df.equals(different_data_type)
12791279
False
12801280
"""
1281-
if not isinstance(other, self._constructor):
1281+
if not (isinstance(other, type(self)) or isinstance(self, type(other))):
12821282
return False
12831283
return self._mgr.equals(other._mgr)
12841284

pandas/tests/frame/test_subclass.py

+8
Original file line numberDiff line numberDiff line change
@@ -696,3 +696,11 @@ def test_idxmax_preserves_subclass(self):
696696
df = tm.SubclassedDataFrame({"A": [1, 2, 3], "B": [4, 5, 6], "C": [7, 8, 9]})
697697
result = df.idxmax()
698698
assert isinstance(result, tm.SubclassedSeries)
699+
700+
def test_equals_subclass(self):
701+
# https://github.com/pandas-dev/pandas/pull/34402
702+
# allow subclass in both directions
703+
df1 = pd.DataFrame({"a": [1, 2, 3]})
704+
df2 = tm.SubclassedDataFrame({"a": [1, 2, 3]})
705+
assert df1.equals(df2)
706+
assert df2.equals(df1)

pandas/tests/series/test_subclass.py

+8
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,11 @@ def test_explode(self):
5151
s = tm.SubclassedSeries([[1, 2, 3], "foo", [], [3, 4]])
5252
result = s.explode()
5353
assert isinstance(result, tm.SubclassedSeries)
54+
55+
def test_equals(self):
56+
# https://github.com/pandas-dev/pandas/pull/34402
57+
# allow subclass in both directions
58+
s1 = pd.Series([1, 2, 3])
59+
s2 = tm.SubclassedSeries([1, 2, 3])
60+
assert s1.equals(s2)
61+
assert s2.equals(s1)

0 commit comments

Comments
 (0)