Skip to content

ENH: add suffixes argument to compare #44365

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

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.4.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ Other enhancements
- :meth:`.GroupBy.mean` now supports `Numba <http://numba.pydata.org/>`_ execution with the ``engine`` keyword (:issue:`43731`)
- :meth:`Timestamp.isoformat`, now handles the ``timespec`` argument from the base :class:``datetime`` class (:issue:`26131`)
- :meth:`NaT.to_numpy` ``dtype`` argument is now respected, so ``np.timedelta64`` can be returned (:issue:`44460`)
- :meth:`DataFrame.compare` now accepts a ``suffixes`` to allow the user to specify the suffixes of both left and right DataFrame which are being compared. This is by default ``self`` and ``other`` (:issue:`44354`)
-


Expand Down
2 changes: 2 additions & 0 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -7297,12 +7297,14 @@ def compare(
align_axis: Axis = 1,
keep_shape: bool = False,
keep_equal: bool = False,
suffixes: Suffixes = ("self", "other"),
Copy link
Contributor

Choose a reason for hiding this comment

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

pls update doc string & add a versionadded.

if we have this in the docs anywhere else (i don't think so but pls check), maybe needs updating

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 update the doc-string, needs a versionadded 1.4. tag

) -> DataFrame:
return super().compare(
other=other,
align_axis=align_axis,
keep_shape=keep_shape,
keep_equal=keep_equal,
suffixes=suffixes,
)

def combine(
Expand Down
5 changes: 3 additions & 2 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
RandomState,
Renamer,
StorageOptions,
Suffixes,
T,
TimedeltaConvertibleTypes,
TimestampConvertibleTypes,
Expand Down Expand Up @@ -8538,6 +8539,7 @@ def compare(
align_axis: Axis = 1,
keep_shape: bool_t = False,
keep_equal: bool_t = False,
suffixes: Suffixes = ("self", "other"),
):
from pandas.core.reshape.concat import concat

Expand All @@ -8548,7 +8550,6 @@ def compare(
)

mask = ~((self == other) | (self.isna() & other.isna()))
keys = ["self", "other"]

if not keep_equal:
self = self.where(mask)
Expand All @@ -8569,7 +8570,7 @@ def compare(
else:
axis = self._get_axis_number(align_axis)

diff = concat([self, other], axis=axis, keys=keys)
diff = concat([self, other], axis=axis, keys=suffixes)
Copy link
Contributor

Choose a reason for hiding this comment

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

these must be a tuple and non-None right? can you add a check and tests the cases (e.g. this is similar to what we checkin merge for example)


if axis >= self.ndim:
# No need to reorganize data if stacking on new axis
Expand Down
17 changes: 17 additions & 0 deletions pandas/tests/frame/methods/test_compare.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,3 +180,20 @@ def test_compare_unaligned_objects():
df1 = pd.DataFrame(np.ones((3, 3)))
df2 = pd.DataFrame(np.zeros((2, 1)))
df1.compare(df2)


def test_compare_suffixes():
# GH
df1 = pd.DataFrame(
{"col1": ["a", "b", "c"], "col2": [1.0, 2.0, np.nan], "col3": [1.0, 2.0, 3.0]},
columns=["col1", "col2", "col3"],
)
df2 = df1.copy()
df2.loc[0, "col1"] = "c"
df2.loc[2, "col3"] = 4.0

suffixes = ["left", "right"]
comp = df1.compare(df2, suffixes=suffixes)

result_suffixes = comp.columns.get_level_values(1).unique()
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 use tm.assert_frame_equal here to assert the results

assert result_suffixes.isin(suffixes).all(), "suffixes not equal"