Skip to content

ENH: Clarify error message when reindexing on an axis containing duplicates #42000

Closed
@aberres

Description

@aberres

Is your feature request related to a problem?

Let's consider the following snippet:

pd.DataFrame(index=[pd.to_datetime("2019"), pd.to_datetime("2019"), pd.to_datetime("2020")]).asfreq("M")

This will raise an exception as the to be resampled dataframe has an axis with duplicates. The problem: I stumpbled at least twice on this problem and never understood what the error message wanted to tell me on first sight.

When seeing just the message (wihtout looking at the backtrace) I always thought this is about providing duplicate axis names or something like this.

ValueError: cannot reindex from a duplicate axis

Describe the solution you'd like

Maybe the exception message could be clarified. The comment two lines above the exception always saves me:

# trying to reindex on an axis with duplicates

So maybe something like this could help?

raise ValueError("Cannot reindex on an axis with duplicate values")

API breaking implications

None I guess.

The message would additionally need to be changed in some tests and in the docs.

Additional context

If there is interest I could draft a PR.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions