Skip to content

BUG: Raise MergeError when suffixes result in duplicate column names … #61422

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v2.3.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ Groupby/resample/rolling

Reshaping
^^^^^^^^^
-
- Bug in :meth:`DataFrame.merge` where user-provided suffixes could result in duplicate column names if the resulting names matched existing columns. Now raises a :class:`MergeError` in such cases. (:issue:`61402`)
Copy link
Member

Choose a reason for hiding this comment

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

Can you move this note to v3.0.0

-

Sparse
Expand Down
12 changes: 7 additions & 5 deletions pandas/core/reshape/merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -3058,17 +3058,19 @@ def renamer(x, suffix: str | None):
llabels = left._transform_index(lrenamer)
rlabels = right._transform_index(rrenamer)

dups = []
dups = set()
if not llabels.is_unique:
# Only warn when duplicates are caused because of suffixes, already duplicated
# columns in origin should not warn
dups = llabels[(llabels.duplicated()) & (~left.duplicated())].tolist()
dups.update(llabels[(llabels.duplicated()) & (~left.duplicated())])
if not rlabels.is_unique:
dups.extend(rlabels[(rlabels.duplicated()) & (~right.duplicated())].tolist())
dups.update(rlabels[(rlabels.duplicated()) & (~right.duplicated())])
# Suffix addition creates duplicate to pre-existing column name
dups.update(llabels.intersection(right.difference(to_rename)))
dups.update(rlabels.intersection(left.difference(to_rename)))
if dups:
raise MergeError(
f"Passing 'suffixes' which cause duplicate columns {set(dups)} is "
f"not allowed.",
f"Passing 'suffixes' which cause duplicate columns {dups} is not allowed.",
)

return llabels, rlabels
15 changes: 15 additions & 0 deletions pandas/tests/reshape/merge/test_merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -3060,3 +3060,18 @@ def test_merge_on_all_nan_column():
{"x": [1, 2, 3], "y": [np.nan, np.nan, np.nan], "z": [4, 5, 6], "zz": [4, 5, 6]}
)
tm.assert_frame_equal(result, expected)


def test_merge_for_suffix_collisions():
# GH#61402
# Case 1: suffixes=("_dup", "") test collision
df1 = DataFrame({"col1": [1], "col2": [2]})
df2 = DataFrame({"col1": [1], "col2": [2], "col2_dup": [3]})
with pytest.raises(MergeError, match="duplicate columns"):
merge(df1, df2, on="col1", suffixes=("_dup", ""))
Copy link
Member

Choose a reason for hiding this comment

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

Can you parametrize this test with

@pytest.mark.parametrize("suffixes", [("_dup", ""), ("", "_dup")])


# Case 2: suffixes=("", "_dup") test collision
df1 = DataFrame({"col1": [1], "col2": [2]})
df2 = DataFrame({"col1": [1], "col2": [2], "col2_dup": [3]})
with pytest.raises(MergeError, match="duplicate columns"):
merge(df1, df2, on="col1", suffixes=("", "_dup"))
Loading