Skip to content

Commit

Permalink
BUG: Join with list of dataframes that have MultiIndex now behaves as…
Browse files Browse the repository at this point in the history
… expected (pandas-dev#57676)
  • Loading branch information
Dacops committed Mar 18, 2024
1 parent 89898a6 commit 0723ff6
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
7 changes: 6 additions & 1 deletion pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -10604,7 +10604,12 @@ def join(
# "Iterable[Union[DataFrame, Series]]" due to the if statements
frames = [cast("DataFrame | Series", self)] + list(other)

can_concat = all(df.index.is_unique for df in frames)
can_concat = True
for df in frames:
if isinstance(df.index, MultiIndex):
can_concat = all(idx.is_unique for idx in df.index.levels)
elif isinstance(df.index, Index) and not df.index.has_duplicates:
can_concat = False

# join indexes only using concat
if can_concat:
Expand Down
18 changes: 18 additions & 0 deletions pandas/tests/frame/methods/test_join.py
Original file line number Diff line number Diff line change
Expand Up @@ -562,3 +562,21 @@ def test_frame_join_tzaware(self):

tm.assert_index_equal(result.index, expected)
assert result.index.tz.zone == "US/Central"

def test_join_list_with_multiindex(self):
test1 = DataFrame(
{"cat": pd.Categorical(["a", "v", "d"])},
index=Index(["a", "b", "c"], name="y"),
)
test2 = DataFrame(
{"foo": np.arange(6)},
index=MultiIndex.from_tuples(
[(0, "a"), (0, "b"), (0, "c"), (1, "a"), (1, "b"), (1, "c")],
names=("x", "y"),
),
)

result = test2.join([test1])
expected = test2.join(test1)

assert result.equals(expected)

0 comments on commit 0723ff6

Please sign in to comment.