-
-
Notifications
You must be signed in to change notification settings - Fork 19.2k
ENH: Implement MultiIndex.insert_level for inserting levels at specified positions #62610
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
Open
Chiwendaiyue
wants to merge
25
commits into
pandas-dev:main
Choose a base branch
from
Chiwendaiyue:shiny-new-feature
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+215
−0
Open
Changes from all commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
e3d6970
FEAT: Add insert_level method to MultiIndex
45ac8ef
FEAT: Add insert_level method to MultiIndex
5b76304
FEAT: Add insert_level method to MultiIndex
5f0caf0
ENH: Add insert_level method to MultiIndex with formatting fixes
97a98e5
STYLE: Format code with ruff
1a9ddc5
FIX: Remove undefined pd reference
2199e6e
Merge branch 'main' into shiny-new-feature
Chiwendaiyue 44985ad
Merge branch 'main' into shiny-new-feature
Chiwendaiyue 9e8676d
Update pandas/core/indexes/multi.py
Chiwendaiyue 094958d
Merge branch 'main' into shiny-new-feature
Chiwendaiyue 77f3af8
DOC: Add whatsnew entry for MultiIndex.insert_level
7bf3067
TEST: Comprehensive consolidation of all test cases into parametrized…
00a346f
Merge branch 'main' into shiny-new-feature
Chiwendaiyue c4ecf7a
FIX: Remove accidental binary file and update whatsnew
8e0068a
FIX: Revert accidental changes to test_query_eval.py
87bd44b
Merge branch 'main' into shiny-new-feature
Chiwendaiyue 79e04b6
PERF: Optimize MultiIndex.insert_level to avoid unnecessary type conv…
1447886
REF: Simplify tuple construction in MultiIndex.insert_level
e2917d0
Merge branch 'main' into shiny-new-feature
Chiwendaiyue 471c2d6
Merge branch 'main' into shiny-new-feature
Chiwendaiyue e2334ac
Merge branch 'main' into shiny-new-feature
Chiwendaiyue eb320fb
Add API reference documentation & Implement insert_level using levels…
9f80ad1
all changes without levels/codes operations
a8d626a
add see also
37f12e6
Merge branch 'main' into shiny-new-feature
Chiwendaiyue 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
Some comments aren't visible on the classic Files Changed page.
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
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
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
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 |
|---|---|---|
| @@ -0,0 +1,151 @@ | ||
| import pytest | ||
|
|
||
| import pandas as pd | ||
| import pandas._testing as tm | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "position, value, name, expected_tuples, expected_names", | ||
| [ | ||
| ( | ||
| 0, | ||
| ["new_value"] * 3, | ||
| None, | ||
| [("new_value", "A", 1), ("new_value", "B", 2), ("new_value", "C", 3)], | ||
| [None, "level1", "level2"], | ||
| ), | ||
| ( | ||
| 1, | ||
| ["middle"] * 3, | ||
| None, | ||
| [("A", "middle", 1), ("B", "middle", 2), ("C", "middle", 3)], | ||
| ["level1", None, "level2"], | ||
| ), | ||
| ( | ||
| 0, | ||
| ["new_val"] * 3, | ||
| "new_level", | ||
| [("new_val", "A", 1), ("new_val", "B", 2), ("new_val", "C", 3)], | ||
| ["new_level", "level1", "level2"], | ||
| ), | ||
| ( | ||
| 1, | ||
| ["middle"] * 3, | ||
| "custom_name", | ||
| [("A", "middle", 1), ("B", "middle", 2), ("C", "middle", 3)], | ||
| ["level1", "custom_name", "level2"], | ||
| ), | ||
| ( | ||
| 0, | ||
| ["start"] * 3, | ||
| None, | ||
| [("start", "A", 1), ("start", "B", 2), ("start", "C", 3)], | ||
| [None, "level1", "level2"], | ||
| ), | ||
| ( | ||
| 2, | ||
| ["end"] * 3, | ||
| None, | ||
| [("A", 1, "end"), ("B", 2, "end"), ("C", 3, "end")], | ||
| ["level1", "level2", None], | ||
| ), | ||
| ( | ||
| 1, | ||
| [100, 100, 100], | ||
| None, | ||
| [("A", 100, 1), ("B", 100, 2), ("C", 100, 3)], | ||
| ["level1", None, "level2"], | ||
| ), | ||
| ( | ||
| 1, | ||
| [1.5, 1.5, 1.5], | ||
| None, | ||
| [("A", 1.5, 1), ("B", 1.5, 2), ("C", 1.5, 3)], | ||
| ["level1", None, "level2"], | ||
| ), | ||
| ( | ||
| 1, | ||
| [None, None, None], | ||
| None, | ||
| [("A", None, 1), ("B", None, 2), ("C", None, 3)], | ||
| ["level1", None, "level2"], | ||
| ), | ||
| ( | ||
| 1, | ||
| ["X", "Y", "Z"], | ||
| None, | ||
| [("A", "X", 1), ("B", "Y", 2), ("C", "Z", 3)], | ||
| ["level1", None, "level2"], | ||
| ), | ||
| ( | ||
| 0, | ||
| [""] * 3, | ||
| "empty_string", | ||
| [("", "A", 1), ("", "B", 2), ("", "C", 3)], | ||
| ["empty_string", "level1", "level2"], | ||
| ), | ||
| ( | ||
| 1, | ||
| [True, True, True], | ||
| None, | ||
| [("A", True, 1), ("B", True, 2), ("C", True, 3)], | ||
| ["level1", None, "level2"], | ||
| ), | ||
| ], | ||
| ) | ||
| def test_insert_level_basic(position, value, name, expected_tuples, expected_names): | ||
| simple_idx = pd.MultiIndex.from_tuples( | ||
| [("A", 1), ("B", 2), ("C", 3)], names=["level1", "level2"] | ||
| ) | ||
|
|
||
| result = simple_idx.insert_level(position, value, name=name) | ||
| expected = pd.MultiIndex.from_tuples(expected_tuples, names=expected_names) | ||
| tm.assert_index_equal(result, expected) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "position, value, expected_error", | ||
| [ | ||
| (5, ["invalid"] * 3, "position must be between"), | ||
| (-1, ["invalid"] * 3, "position must be between"), | ||
| (1, ["too", "few"], "Length of values must match"), | ||
| (3, ["value"] * 3, "position must be between"), | ||
| (0, "scalar_value", "value must be an array-like object"), | ||
| ], | ||
| ) | ||
| def test_insert_level_error_cases(position, value, expected_error): | ||
| simple_idx = pd.MultiIndex.from_tuples( | ||
| [("A", 1), ("B", 2), ("C", 3)], names=["level1", "level2"] | ||
| ) | ||
|
|
||
| with pytest.raises(ValueError, match=expected_error): | ||
| simple_idx.insert_level(position, value) | ||
|
|
||
|
|
||
| def test_insert_level_preserves_original(): | ||
| simple_idx = pd.MultiIndex.from_tuples( | ||
| [("A", 1), ("B", 2), ("C", 3)], names=["level1", "level2"] | ||
| ) | ||
|
|
||
| original = simple_idx.copy() | ||
| simple_idx.insert_level(1, ["temp"] * 3) | ||
|
|
||
| tm.assert_index_equal(original, simple_idx) | ||
|
|
||
|
|
||
| def test_insert_level_empty_index(): | ||
| empty_idx = pd.MultiIndex.from_tuples([], names=["level1", "level2"]) | ||
|
|
||
| result = empty_idx.insert_level(0, []) | ||
| expected = pd.MultiIndex.from_tuples([], names=[None, "level1", "level2"]) | ||
| tm.assert_index_equal(result, expected) | ||
|
|
||
|
|
||
| def test_insert_level_single_element(): | ||
| single_idx = pd.MultiIndex.from_tuples([("A", 1)], names=["level1", "level2"]) | ||
|
|
||
| result = single_idx.insert_level(1, ["middle"]) | ||
| expected = pd.MultiIndex.from_tuples( | ||
| [("A", "middle", 1)], names=["level1", None, "level2"] | ||
| ) | ||
| tm.assert_index_equal(result, expected) |
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 put
insert_levelin the API reference?