From 63d7825c0054d53341e6b6983782b4052702ecfc Mon Sep 17 00:00:00 2001 From: Joris Van den Bossche Date: Wed, 27 May 2020 09:40:48 +0200 Subject: [PATCH 1/5] BUG/API: other object type check in Series/DataFrame.equals --- pandas/core/generic.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index 79805bec85af0..c68edffc96678 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -1324,7 +1324,7 @@ def equals(self, other): >>> df.equals(different_data_type) False """ - if not isinstance(other, self._constructor): + if not isinstance(other, type(self)): return False return self._mgr.equals(other._mgr) From f148449620996e4d27a095f8f1519d182e5097fd Mon Sep 17 00:00:00 2001 From: Joris Van den Bossche Date: Fri, 29 May 2020 15:48:38 +0200 Subject: [PATCH 2/5] allow subclass + add test --- pandas/core/generic.py | 2 +- pandas/tests/generic/test_generic.py | 13 +++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index c01b481b7709b..b85d5aa2192c0 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -1324,7 +1324,7 @@ def equals(self, other): >>> df.equals(different_data_type) False """ - if not isinstance(other, type(self)): + if not (isinstance(other, type(self)) or isinstance(self, type(other))): return False return self._mgr.equals(other._mgr) diff --git a/pandas/tests/generic/test_generic.py b/pandas/tests/generic/test_generic.py index 05588ead54be4..4cc30749c39ca 100644 --- a/pandas/tests/generic/test_generic.py +++ b/pandas/tests/generic/test_generic.py @@ -872,6 +872,19 @@ def test_equals(self): assert a.equals(e) assert e.equals(f) + def test_equals_subclass(self): + # https://github.com/pandas-dev/pandas/pull/34402 + + class MySeries(pd.Series): + pass + + s1 = pd.Series([1, 2, 3]) + s2 = MySeries([1, 2, 3]) + + # allow subclass in both directions + assert s1.equals(s2) + assert s2.equals(s1) + def test_pipe(self): df = DataFrame({"A": [1, 2, 3]}) f = lambda x, y: x ** y From 348f70683603bdfa2a535a06b90a491419ddb2a2 Mon Sep 17 00:00:00 2001 From: Joris Van den Bossche Date: Fri, 19 Jun 2020 11:55:43 +0200 Subject: [PATCH 3/5] use SubclassedSeries --- pandas/tests/generic/test_generic.py | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/pandas/tests/generic/test_generic.py b/pandas/tests/generic/test_generic.py index 4cc30749c39ca..51ae0bf43b621 100644 --- a/pandas/tests/generic/test_generic.py +++ b/pandas/tests/generic/test_generic.py @@ -874,12 +874,8 @@ def test_equals(self): def test_equals_subclass(self): # https://github.com/pandas-dev/pandas/pull/34402 - - class MySeries(pd.Series): - pass - s1 = pd.Series([1, 2, 3]) - s2 = MySeries([1, 2, 3]) + s2 = tm.SubclassedSeries([1, 2, 3]) # allow subclass in both directions assert s1.equals(s2) From 17592217bd2649cdce492f2bf1a75f271c8bafed Mon Sep 17 00:00:00 2001 From: Joris Van den Bossche Date: Fri, 19 Jun 2020 11:58:28 +0200 Subject: [PATCH 4/5] add whatsnew --- doc/source/whatsnew/v1.1.0.rst | 2 ++ 1 file changed, 2 insertions(+) diff --git a/doc/source/whatsnew/v1.1.0.rst b/doc/source/whatsnew/v1.1.0.rst index a27e6e8433779..28f742d9b9756 100644 --- a/doc/source/whatsnew/v1.1.0.rst +++ b/doc/source/whatsnew/v1.1.0.rst @@ -1095,6 +1095,8 @@ Other - Bug in :meth:`DataFrame.__dir__` caused a segfault when using unicode surrogates in a column name (:issue:`25509`) - Bug in :meth:`DataFrame.plot.scatter` caused an error when plotting variable marker sizes (:issue:`32904`) - :class:`IntegerArray` now implements the ``sum`` operation (:issue:`33172`) +- Bug in :meth:`DataFrame.equals` and :meth:`Series.equals` in allowing subclasses + to be equal (:issue:`34402`). - Bug in :class:`Tick` comparisons raising ``TypeError`` when comparing against timedelta-like objects (:issue:`34088`) - Bug in :class:`Tick` multiplication raising ``TypeError`` when multiplying by a float (:issue:`34486`) From 66293a53a6677dd6a3d3abca5bda959443866641 Mon Sep 17 00:00:00 2001 From: Joris Van den Bossche Date: Fri, 3 Jul 2020 21:12:35 +0200 Subject: [PATCH 5/5] add dataframe test --- pandas/tests/generic/test_generic.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/pandas/tests/generic/test_generic.py b/pandas/tests/generic/test_generic.py index 4829a9b1f92bb..c85b3c832ee87 100644 --- a/pandas/tests/generic/test_generic.py +++ b/pandas/tests/generic/test_generic.py @@ -874,13 +874,18 @@ def test_equals(self): def test_equals_subclass(self): # https://github.com/pandas-dev/pandas/pull/34402 + # allow subclass in both directions + s1 = pd.Series([1, 2, 3]) s2 = tm.SubclassedSeries([1, 2, 3]) - - # allow subclass in both directions assert s1.equals(s2) assert s2.equals(s1) + df1 = pd.DataFrame({"a": [1, 2, 3]}) + df2 = tm.SubclassedDataFrame({"a": [1, 2, 3]}) + assert df1.equals(df2) + assert df2.equals(df1) + def test_pipe(self): df = DataFrame({"A": [1, 2, 3]}) f = lambda x, y: x ** y