Skip to content

BUG: Join ValueError for DataFrame list #46630

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 17 commits into from
Apr 7, 2022
Merged
Show file tree
Hide file tree
Changes from 13 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.5.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -619,6 +619,7 @@ Styler

Other
^^^^^
- Bug in :meth:`DataFrame.join` with a list when using suffixes to join DataFrames with duplicate column names (:issue:`46396`)

.. ***DO NOT USE THIS SECTION***

Expand Down
5 changes: 5 additions & 0 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -9576,6 +9576,11 @@ def _join_compat(
"Joining multiple DataFrames only supported for joining on index"
)

if rsuffix or lsuffix:
raise ValueError(
"Suffixes not supported when joining multiple DataFrames"
)

frames = [self] + list(other)

can_concat = all(df.index.is_unique for df in frames)
Expand Down
22 changes: 22 additions & 0 deletions pandas/tests/frame/methods/test_join.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,28 @@ def test_join(left, right, how, sort, expected):
tm.assert_frame_equal(result, expected)


def test_suffix_on_list_join():
first = DataFrame({"key": [1, 2, 3, 4, 5]})
second = DataFrame({"key": [1, 8, 3, 2, 5], "v1": [1, 2, 3, 4, 5]})
third = DataFrame({"keys": [5, 2, 3, 4, 1], "v2": [1, 2, 3, 4, 5]})

# check proper errors are raised
msg = "Suffixes not supported when joining multiple DataFrames"
with pytest.raises(ValueError, match=msg):
first.join([second], lsuffix="y")
with pytest.raises(ValueError, match=msg):
first.join([second, third], rsuffix="x")
with pytest.raises(ValueError, match=msg):
first.join([second, third], lsuffix="y", rsuffix="x")
with pytest.raises(ValueError, match="Indexes have overlapping values"):
first.join([second, third])

# no errors should be raised
arr_joined = first.join([third])
norm_joined = first.join(third)
tm.assert_frame_equal(arr_joined, norm_joined)


def test_join_index(float_frame):
# left / right

Expand Down