Skip to content

ENH: unset_index method #60869

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

Open
1 of 3 tasks
zkurtz opened this issue Feb 6, 2025 · 0 comments
Open
1 of 3 tasks

ENH: unset_index method #60869

zkurtz opened this issue Feb 6, 2025 · 0 comments
Labels
Enhancement Needs Triage Issue that has not been reviewed by a pandas team member

Comments

@zkurtz
Copy link

zkurtz commented Feb 6, 2025

Feature Type

  • Adding new functionality to pandas

  • Changing existing functionality in pandas

  • Removing existing functionality in pandas

Problem Description

In many contexts I've had the need to "unset" a data frame index prior to passing the data frame to an index-blind method. My main requirements for such an index unset are:

  • the output should have the most trivial index possible, i.e. an unnamed range index
  • if the input has only an unnamed range index, the "unset" operation should be a no-op
  • if the input has a named index, it should be kept as a new column (or columns for a multi-index)
  • if the input is not a simple RangeIndex and is unnamed, raise a value error to remind me to decide whether that index is supposed to be meaningful (typically I'd name it if so, or do .reset_index(drop=True) if not).

Usage of the .reset_index method to cover all of these requirements in a general sense requires a bit of nuance:

  • calling df.reset_index() introduces a new column named simply "index" when users forget to use keep=False even when the index is [trivial] unnamed range index. In a stricter/safer world, you might want an error instead when you're about to construct a data frame column from an unnamed index.
  • the requirement to specify keep at all is sometime onerous; it typically would be preferable to have a method that's a no-op in case the existing index is already an unnamed range index.

Feature Description

Define a new unset_index method on DataFrame something like

def unset_index(self) -> Self:
    if is_unnamed_range_index(df.index):
        return df
    if index_has_any_unnamed_col(df.index):
        raise ValueError(
            "At least one column of the index is unnamed while the index itself is not a RangeIndex. "
            "Set the names of the index columns before calling unset, or just call reset_index(drop=True) directly."
        )
    return df.reset_index(drop=False, allow_duplicates=False)

Alternative Solutions

Until something like this is available in pandas, I'm using the unset method in pandahandler.

Additional Context

No response

@zkurtz zkurtz added Enhancement Needs Triage Issue that has not been reviewed by a pandas team member labels Feb 6, 2025
@zkurtz zkurtz changed the title ENH: keep="safe" option for reset_index method ENH: unset_index method Feb 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Enhancement Needs Triage Issue that has not been reviewed by a pandas team member
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant