Skip to content

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

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

Closed
aberres opened this issue Jun 14, 2021 · 2 comments · Fixed by #42007
Closed

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

aberres opened this issue Jun 14, 2021 · 2 comments · Fixed by #42007
Labels
Enhancement Error Reporting Incorrect or improved errors from pandas

Comments

@aberres
Copy link
Contributor

aberres commented Jun 14, 2021

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.

@aberres aberres added Enhancement Needs Triage Issue that has not been reviewed by a pandas team member labels Jun 14, 2021
@simonjayhawkins simonjayhawkins added Error Reporting Incorrect or improved errors from pandas and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Jun 14, 2021
@simonjayhawkins simonjayhawkins added this to the Contributions Welcome milestone Jun 14, 2021
@simonjayhawkins
Copy link
Member

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

maybe "Cannot reindex on an axis with duplicate labels"

@jreback
Copy link
Contributor

jreback commented Jun 15, 2021

I think there maybe an issue about this that duplicates this one if you can have a search.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Enhancement Error Reporting Incorrect or improved errors from pandas
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants