pandas icon indicating copy to clipboard operation
pandas copied to clipboard

ENH: Implement MultiIndex.insert_level for inserting levels at specified positions

Open Chiwendaiyue opened this issue 2 months ago • 5 comments

  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')

Chiwendaiyue avatar Oct 07 '25 13:10 Chiwendaiyue

@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!

Chiwendaiyue avatar Oct 21 '25 10:10 Chiwendaiyue

It needs the approval of a core member. Just wait.

Alvaro-Kothe avatar Oct 21 '25 11:10 Alvaro-Kothe

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!

Chiwendaiyue avatar Oct 26 '25 04:10 Chiwendaiyue

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!

Chiwendaiyue avatar Nov 02 '25 05:11 Chiwendaiyue

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.

Chiwendaiyue avatar Nov 16 '25 11:11 Chiwendaiyue