Skip to content

Conversation

Chiwendaiyue
Copy link

@Chiwendaiyue Chiwendaiyue commented Oct 7, 2025

  1. What does this PR do?

Adds insert_level method to MultiIndex for inserting new levels at specified positions.

  1. Motivation

This addresses the feature request in issue #62558 for a simple way to add levels to MultiIndex at given positions.

  1. Key changes
  • Implement insert_level method in pandas/core/indexes/multi.py
  • Add comprehensive test suite in pandas/tests/indexes/multi/test_insert_level.py
  • Handle various edge cases and error conditions
  1. Testing
  • All new tests pass
  • Existing functionality remains unchanged
  • Handles scalar values and array-like inputs
  • Proper error handling for invalid inputs
  1. Example usage

idx = pd.MultiIndex.from_tuples([('A', 1), ('B', 2)])
result = idx.insert_level(1, 'new_level')

cloudboat added 2 commits October 7, 2025 20:43
- 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
@Chiwendaiyue Chiwendaiyue marked this pull request as draft October 7, 2025 13:05
@Chiwendaiyue Chiwendaiyue marked this pull request as ready for review October 7, 2025 13:18
@Chiwendaiyue Chiwendaiyue changed the title multiindex-insert-level ENH: Implement MultiIndex.insert_level for inserting levels at specified positions Oct 7, 2025
@Chiwendaiyue Chiwendaiyue marked this pull request as draft October 7, 2025 13:29
cloudboat added 4 commits October 7, 2025 21:57
- 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant