Skip to content

BUG/API: other object type check in Series/DataFrame.equals #34402

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Jul 15, 2020
2 changes: 1 addition & 1 deletion pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)) or isinstance(self, type(other))):
return False
return self._mgr.equals(other._mgr)

Expand Down
13 changes: 13 additions & 0 deletions pandas/tests/generic/test_generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you put this in tests/series/test_sublcass and similar for frame/test_sublcass

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not a test about the subclass working correctly (anything related to the subclassing machinery, eg correct use of _constructor to preserve the subclass in operations etc), but only about our own equals method accepting subclasses, so I think the test rather fits here with the other equals tests.

But I can certainly use the tm.SubclassedSeries helper to avoid creating a dummy class here.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think this belongs with the rest of the tests & needs for DataFrame subclass as well. having 2 places is just very friendly to future folks.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Having 2 places where equals is tested is equally unfriendly I think ..
Both ways have some aspect of the tests splitted in two places, and as mentioned above, this test is actually about our equals implementation, not about the behaviour of a subclassing mechanism, and thus more belongs here IMO.

Will add dataframe to the test

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pls just move this, ALL subclass tests are in the same file. This is breaking the pattern for no reason.

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
Expand Down