-
-
Notifications
You must be signed in to change notification settings - Fork 19.1k
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
base: main
Are you sure you want to change the base?
Changes from 11 commits
e3d6970
45ac8ef
5b76304
5f0caf0
97a98e5
1a9ddc5
2199e6e
44985ad
9e8676d
094958d
77f3af8
7bf3067
00a346f
c4ecf7a
8e0068a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The changes in this file seems unrelated. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry, It might be remnants of a merge conflict - looks like an issue from resolving conflicts.I have made it the same as the main branch. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
import pytest | ||
|
||
import pandas as pd | ||
import pandas._testing as tm | ||
|
||
|
||
class TestMultiIndexInsertLevel: | ||
@pytest.mark.parametrize( | ||
"position, value, name, expected_tuples, expected_names", | ||
[ | ||
( | ||
0, | ||
"new_value", | ||
None, | ||
[("new_value", "A", 1), ("new_value", "B", 2), ("new_value", "C", 3)], | ||
[None, "level1", "level2"], | ||
), | ||
( | ||
1, | ||
"middle", | ||
None, | ||
[("A", "middle", 1), ("B", "middle", 2), ("C", "middle", 3)], | ||
["level1", None, "level2"], | ||
), | ||
( | ||
0, | ||
"new_val", | ||
"new_level", | ||
[("new_val", "A", 1), ("new_val", "B", 2), ("new_val", "C", 3)], | ||
["new_level", "level1", "level2"], | ||
), | ||
( | ||
1, | ||
"middle", | ||
"custom_name", | ||
[("A", "middle", 1), ("B", "middle", 2), ("C", "middle", 3)], | ||
["level1", "custom_name", "level2"], | ||
), | ||
], | ||
) | ||
def test_insert_level_basic( | ||
self, 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", | ||
[ | ||
(0, "start"), | ||
(2, "end"), | ||
], | ||
) | ||
def test_insert_level_edge_positions(self, position, value): | ||
|
||
simple_idx = pd.MultiIndex.from_tuples( | ||
[("A", 1), ("B", 2), ("C", 3)], names=["level1", "level2"] | ||
) | ||
|
||
result = simple_idx.insert_level(position, value) | ||
assert result.nlevels == 3 | ||
|
||
@pytest.mark.parametrize( | ||
"position, value, expected_error", | ||
[ | ||
(5, "invalid", "position must be between"), | ||
(-1, "invalid", "position must be between"), | ||
(1, ["too", "few"], "Length of values must match"), | ||
], | ||
) | ||
def test_insert_level_error_cases(self, 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) | ||
|
||
@pytest.mark.parametrize( | ||
"value", | ||
[100, 1.5, None], | ||
) | ||
def test_insert_level_with_different_data_types(self, value): | ||
|
||
simple_idx = pd.MultiIndex.from_tuples( | ||
[("A", 1), ("B", 2), ("C", 3)], names=["level1", "level2"] | ||
) | ||
|
||
result = simple_idx.insert_level(1, value) | ||
assert result.nlevels == 3 | ||
|
||
def test_insert_level_preserves_original(self): | ||
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") | ||
|
||
tm.assert_index_equal(original, simple_idx) | ||
|
||
def test_insert_level_empty_index(self): | ||
empty_idx = pd.MultiIndex.from_tuples([], names=["level1", "level2"]) | ||
|
||
result = empty_idx.insert_level(0, []) | ||
assert result.nlevels == 3 | ||
assert len(result) == 0 | ||
|
||
def test_insert_level_with_different_values(self): | ||
simple_idx = pd.MultiIndex.from_tuples( | ||
[("A", 1), ("B", 2), ("C", 3)], names=["level1", "level2"] | ||
) | ||
|
||
new_values = ["X", "Y", "Z"] | ||
result = simple_idx.insert_level(1, new_values) | ||
expected = pd.MultiIndex.from_tuples( | ||
[("A", "X", 1), ("B", "Y", 2), ("C", "Z", 3)], | ||
names=["level1", None, "level2"], | ||
) | ||
tm.assert_index_equal(result, expected) |
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think you committed this by accident |
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.
You don't need to remove the
-
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.
Sorry, I fixed it