-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -53,6 +53,7 @@ | |
RandomState, | ||
Renamer, | ||
StorageOptions, | ||
Suffixes, | ||
T, | ||
TimedeltaConvertibleTypes, | ||
TimestampConvertibleTypes, | ||
|
@@ -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 | ||
|
||
|
@@ -8548,7 +8550,6 @@ def compare( | |
) | ||
|
||
mask = ~((self == other) | (self.isna() & other.isna())) | ||
keys = ["self", "other"] | ||
|
||
if not keep_equal: | ||
self = self.where(mask) | ||
|
@@ -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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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" |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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