-
-
Notifications
You must be signed in to change notification settings - Fork 19.4k
TST: Add regression test for MultiIndex merge after reset_index #63256
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
mroeschke
merged 2 commits into
pandas-dev:main
from
zachyattack23:test-gh62150-multiindex-merge
Dec 5, 2025
+35
−0
Merged
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2936,6 +2936,44 @@ def test_merge_multiindex_single_level(): | |
| tm.assert_frame_equal(result, expected) | ||
|
|
||
|
|
||
| def test_merge_multiindex_reset_index_mixed(): | ||
| # GH#62150 - merging reset MultiIndex when one df has single-level index | ||
| # Bug existed in 2.3.1, fixed in 3.0.0, this test prevents regression | ||
|
|
||
| # Create first dataframe with MultiIndex on both index and columns | ||
| df = DataFrame(data={("column_1", ""): [1, 1], ("column_2", ""): [2, 2]}) | ||
| df.index = MultiIndex.from_arrays( | ||
| [[1, 1], ["metadata_1", "metadata_2"]], names=["index", "metadata"] | ||
| ) | ||
|
|
||
| # Create second dataframe with single index and MultiIndex columns | ||
| df2 = DataFrame(data=[1, 1], index=[1, 1]).rename_axis("index", axis=0) | ||
| df2.columns = MultiIndex.from_product([["new_data"], [""]]) | ||
|
||
|
|
||
| # Test merge with on='index' - should work | ||
| # Note: PerformanceWarning is expected due to non-lexsorted MultiIndex | ||
| with tm.assert_produces_warning(pd.errors.PerformanceWarning): | ||
| result = df.reset_index().merge(df2.reset_index(), on="index") | ||
|
|
||
| expected = DataFrame( | ||
| { | ||
| ("index", ""): [1, 1, 1, 1], | ||
| ("metadata", ""): ["metadata_1", "metadata_1", "metadata_2", "metadata_2"], | ||
| ("column_1", ""): [1, 1, 1, 1], | ||
| ("column_2", ""): [2, 2, 2, 2], | ||
| ("new_data", ""): [1, 1, 1, 1], | ||
| } | ||
| ) | ||
| expected.columns = MultiIndex.from_tuples(expected.columns) | ||
|
|
||
| tm.assert_frame_equal(result, expected) | ||
|
|
||
| # Test merge with on=[('index', '')] - should also work | ||
| # Warning may or may not be raised depending on internal state | ||
| result2 = df.reset_index().merge(df2.reset_index(), on=[("index", "")]) | ||
| tm.assert_frame_equal(result2, expected) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("on_index", [True, False]) | ||
| @pytest.mark.parametrize("left_unique", [True, False]) | ||
| @pytest.mark.parametrize("left_monotonic", [True, False]) | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you remove all the comments (and the comments below) except
GH#62150?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure