-
-
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?
Conversation
- Implement insert_level method for MultiIndex to insert new levels at specified positions - Add comprehensive test cases for the new functionality - Fix level names handling to match expected behavior Resolves: MultiIndex level insertion feature request
- Implement insert_level method for MultiIndex to insert new levels at specified positions - Add comprehensive test cases for the new functionality - Fix level names handling to match expected behavior Resolves: MultiIndex level insertion feature request
- Implement insert_level method for MultiIndex to insert new levels at specified positions - Add comprehensive test cases for the new functionality - Fix level names handling to match expected behavior Resolves: MultiIndex level insertion feature request
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.
Thanks for the PR. Here are some suggestions against the test suite.
It seems that you committed fastparquet
and pyarrow
accidentally.
Also, can you add an entry to whatsnew?
def setup_method(self): | ||
self.simple_idx = pd.MultiIndex.from_tuples( | ||
[("A", 1), ("B", 2), ("C", 3)], names=["level1", "level2"] | ||
) | ||
self.empty_idx = pd.MultiIndex.from_tuples([], names=["level1", "level2"]) |
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.
NIT: I would prefer that you define this in the test body that should use it.
) | ||
self.empty_idx = pd.MultiIndex.from_tuples([], names=["level1", "level2"]) | ||
|
||
def test_insert_level_basic(self): |
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 parametrize this test?
result = self.simple_idx.insert_level(0, "new_val", name="new_level") | ||
assert result.names[0] == "new_level" | ||
|
||
def test_insert_level_edge_positions(self): |
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.
This can go into the first test.
result_end = self.simple_idx.insert_level(2, "end") | ||
assert result_end.nlevels == 3 | ||
|
||
def test_insert_level_error_cases(self): |
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 parametrize this?
with pytest.raises(ValueError, match="Length of values must match"): | ||
self.simple_idx.insert_level(1, ["too", "few"]) | ||
|
||
def test_insert_level_with_different_data_types(self): |
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.
These tests could go into the first test too.
def test_debug_names(): | ||
idx = pd.MultiIndex.from_tuples( | ||
[("A", 1), ("B", 2), ("C", 3)], names=["level1", "level2"] | ||
) | ||
print("Original names:", idx.names) | ||
|
||
result = idx.insert_level(0, "new_value") | ||
print("Result names:", result.names) | ||
|
||
expected = pd.MultiIndex.from_tuples( | ||
[("new_value", "A", 1), ("new_value", "B", 2), ("new_value", "C", 3)], | ||
names=[None, "level1", "level2"], | ||
) | ||
print("Expected names:", expected.names) |
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.
Why does this exist?
|
||
tm.assert_index_equal(original, self.simple_idx) | ||
|
||
assert result.nlevels == original.nlevels + 1 |
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.
This assertion feels redundant against the first test.
def test_insert_level_with_name(self): | ||
result = self.simple_idx.insert_level(0, "new_val", name="new_level") | ||
assert result.names[0] == "new_level" |
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 could also test adding names in the first test and remove this test.
Co-authored-by: Álvaro Kothe <[email protected]>
Adds
insert_level
method to MultiIndex for inserting new levels at specified positions.This addresses the feature request in issue #62558 for a simple way to add levels to MultiIndex at given positions.
insert_level
method inpandas/core/indexes/multi.py
pandas/tests/indexes/multi/test_insert_level.py
idx = pd.MultiIndex.from_tuples([('A', 1), ('B', 2)])
result = idx.insert_level(1, 'new_level')