Skip to content

BUG: created check and warning for merging dataframes on unequal inde… #32796

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

Closed
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
13 changes: 11 additions & 2 deletions pandas/core/reshape/merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -606,10 +606,19 @@ def __init__(
f"right_index parameter must be of type bool, not {type(right_index)}"
)

# warn user when merging between different levels
# warn users when merging between different index levels
if _left.index.nlevels != _right.index.nlevels:
msg = (
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 add a comment `# GH#[relevant_issue]"

"merging between different index levels can give an unintended "
f"result ({left.index.nlevels} levels on the left,"
f"{right.index.nlevels} on the right)"
)
warnings.warn(msg, UserWarning)

# warn user when merging between different column levels
if _left.columns.nlevels != _right.columns.nlevels:
msg = (
"merging between different levels can give an unintended "
"merging between different column levels can give an unintended "
f"result ({left.columns.nlevels} levels on the left,"
f"{right.columns.nlevels} on the right)"
)
Expand Down
26 changes: 26 additions & 0 deletions pandas/tests/frame/test_axis_select_reindex.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,32 @@ def test_drop_api_equivalence(self):
with pytest.raises(ValueError):
df.drop(axis=1)

def test_merge_join_different_index_levels(self):
#GH 13094
Copy link
Member

Choose a reason for hiding this comment

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

space after "#", can fix with black pandas


df1 = DataFrame([[2, 3], [5, 7]], columns=['a', 'p']).set_index('a')

df2 = DataFrame([[1, 2, 3], [3, 4, 8], [5, 6, 9]],
columns=['a', 'b', 'c']).set_index(['a', 'b'])

# join
columns = ['a', 'b', 'p', 'c']
expected = DataFrame([[5, 6, 7, 9]], columns=columns).set_index(['a', 'b'])

with tm.assert_produces_warning(UserWarning):
result = df1.join(df2, how='left')

tm.assert_frame_equal(result, expected)

# merge
columns = ['a', 'p', 'c']
expected = DataFrame([[5, 7, 9]], columns=columns).set_index('a')

with tm.assert_produces_warning(UserWarning):
result = pd.merge(df1, df2, on='a')

tm.assert_frame_equal(result, expected)

def test_merge_join_different_levels(self):
# GH 9455

Expand Down