Skip to content

Commit cb0611a

Browse files
committed
BUG: Join with list of dataframes that have MultiIndex now behaves as expected (#57676)
1 parent 89898a6 commit cb0611a

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

pandas/core/frame.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -10604,7 +10604,12 @@ def join(
1060410604
# "Iterable[Union[DataFrame, Series]]" due to the if statements
1060510605
frames = [cast("DataFrame | Series", self)] + list(other)
1060610606

10607-
can_concat = all(df.index.is_unique for df in frames)
10607+
can_concat = True
10608+
for df in frames:
10609+
if isinstance(df.index, MultiIndex):
10610+
can_concat = all(idx.is_unique for idx in df.index.levels)
10611+
elif isinstance(df.index, Index) and not df.index.has_duplicates:
10612+
can_concat = False
1060810613

1060910614
# join indexes only using concat
1061010615
if can_concat:

pandas/tests/frame/methods/test_join.py

+18
Original file line numberDiff line numberDiff line change
@@ -562,3 +562,21 @@ def test_frame_join_tzaware(self):
562562

563563
tm.assert_index_equal(result.index, expected)
564564
assert result.index.tz.zone == "US/Central"
565+
566+
def test_join_list_with_multiindex(self):
567+
test1 = DataFrame(
568+
{"cat": pd.Categorical(["a", "v", "d"])},
569+
index=Index(["a", "b", "c"], name="y"),
570+
)
571+
test2 = DataFrame(
572+
{"foo": np.arange(6)},
573+
index=MultiIndex.from_tuples(
574+
[(0, "a"), (0, "b"), (0, "c"), (1, "a"), (1, "b"), (1, "c")],
575+
names=("x", "y"),
576+
),
577+
)
578+
579+
result = test2.join([test1])
580+
expected = test2.join(test1)
581+
582+
assert result.equals(expected)

0 commit comments

Comments
 (0)