ENH: Implement MultiIndex.insert_level for inserting levels at specified positions
- What does this PR do?
Adds insert_level method to MultiIndex for inserting new levels at specified positions.
- Motivation
This addresses the feature request in issue #62558 for a simple way to add levels to MultiIndex at given positions.
- Key changes
- Implement
insert_levelmethod inpandas/core/indexes/multi.py - Add comprehensive test suite in
pandas/tests/indexes/multi/test_insert_level.py - Handle various edge cases and error conditions
- Testing
- All new tests pass
- Existing functionality remains unchanged
- Handles scalar values and array-like inputs
- Proper error handling for invalid inputs
- Example usage
idx = pd.MultiIndex.from_tuples([('A', 1), ('B', 2)]) result = idx.insert_level(1, 'new_level')
@Alvaro-Kothe Just checking if there's anything else needed before this can be merged? All checks are passing and the feature is ready. Thanks!
It needs the approval of a core member. Just wait.
Hi @mroeschke Just a friendly follow-up on the MultiIndex.insert_level implementation. I'm still waiting for your guidance on whether to use the simple from_tuples approach (currently working). Would you mind taking a look when you have a moment? Thanks!
Hi @mroeschke Just a friendly follow-up on the MultiIndex.insert_level implementation. I'm still waiting for your review for the code changes, if the strategy is acceptable. Thanks!
Hi @mroeschke Just a gentle reminder about the MultiIndex.insert_level PR. It's been fully implemented with:
- Complete test coverage
- API documentation
- Type annotations
All the tests were passed, would appreciate a review when you have time! Happy to address any feedback.