Skip to content

Commit e1b95fa

Browse files
authored
BUG: Join ValueError for DataFrame list (#46630)
1 parent 66c300b commit e1b95fa

File tree

3 files changed

+28
-0
lines changed

3 files changed

+28
-0
lines changed

doc/source/whatsnew/v1.5.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -604,6 +604,7 @@ Reshaping
604604
- Bug in :meth:`DataFrame.align` when aligning a :class:`MultiIndex` to a :class:`Series` with another :class:`MultiIndex` (:issue:`46001`)
605605
- Bug in concanenation with ``IntegerDtype``, or ``FloatingDtype`` arrays where the resulting dtype did not mirror the behavior of the non-nullable dtypes (:issue:`46379`)
606606
- Bug in :func:`concat` with identical key leads to error when indexing :class:`MultiIndex` (:issue:`46519`)
607+
- Bug in :meth:`DataFrame.join` with a list when using suffixes to join DataFrames with duplicate column names (:issue:`46396`)
607608
-
608609

609610
Sparse

pandas/core/frame.py

+5
Original file line numberDiff line numberDiff line change
@@ -9576,6 +9576,11 @@ def _join_compat(
95769576
"Joining multiple DataFrames only supported for joining on index"
95779577
)
95789578

9579+
if rsuffix or lsuffix:
9580+
raise ValueError(
9581+
"Suffixes not supported when joining multiple DataFrames"
9582+
)
9583+
95799584
frames = [self] + list(other)
95809585

95819586
can_concat = all(df.index.is_unique for df in frames)

pandas/tests/frame/methods/test_join.py

+22
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,28 @@ def test_join(left, right, how, sort, expected):
8282
tm.assert_frame_equal(result, expected)
8383

8484

85+
def test_suffix_on_list_join():
86+
first = DataFrame({"key": [1, 2, 3, 4, 5]})
87+
second = DataFrame({"key": [1, 8, 3, 2, 5], "v1": [1, 2, 3, 4, 5]})
88+
third = DataFrame({"keys": [5, 2, 3, 4, 1], "v2": [1, 2, 3, 4, 5]})
89+
90+
# check proper errors are raised
91+
msg = "Suffixes not supported when joining multiple DataFrames"
92+
with pytest.raises(ValueError, match=msg):
93+
first.join([second], lsuffix="y")
94+
with pytest.raises(ValueError, match=msg):
95+
first.join([second, third], rsuffix="x")
96+
with pytest.raises(ValueError, match=msg):
97+
first.join([second, third], lsuffix="y", rsuffix="x")
98+
with pytest.raises(ValueError, match="Indexes have overlapping values"):
99+
first.join([second, third])
100+
101+
# no errors should be raised
102+
arr_joined = first.join([third])
103+
norm_joined = first.join(third)
104+
tm.assert_frame_equal(arr_joined, norm_joined)
105+
106+
85107
def test_join_index(float_frame):
86108
# left / right
87109

0 commit comments

Comments
 (0)