Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

BUG: Raise clear error for duplicate id_vars in melt (GH61475)#61484

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged

Conversation

ZanirP
Copy link
Contributor

@ZanirPZanirP commentedMay 23, 2025
edited
Loading

Comment on lines 242 to 244
# GH61475 - prevent AttributeError when duplicate column
if not hasattr(id_data, "dtype"):
raise Exception(f"{col} is a duplicate column header")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

  1. This should checkif not frame.columns.is_unique at the beginning of the function.
  2. AValueError is more appropriate here

Copy link
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Thanks for the feedback! I've moved the check fornot frame.columns.is_unique to the beginning of the function and updated the exception type toValueError as suggested.

A quick clarification question: currentlymelt allows duplicate column names in 'value_vars', as seen in the testtest_melt_with_duplicate_columns.With this change, are we treating any duplicate columns in the inputDataFrame as aValueError? Not just when the duplicates appear inid_vars?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Ah good point. I guess this specifically whenid_vars is not empty we'll want to raiseif not frame.columns.is_unique

Copy link
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Thanks! Just to make sure I understand:
You're saying that we should only raise an error in duplicate column names if the duplicate is inid_vars, correct? Essentially if the duplicates are invalue_vars then, we can let the melt function work as is, as long as no errors are occuring?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Yup, correct.

@mroeschkemroeschke added ReshapingConcat, Merge/Join, Stack/Unstack, Explode Error ReportingIncorrect or improved errors from pandas labelsMay 23, 2025
Copy link
ContributorAuthor

@ZanirPZanirP left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

I have updated logic to raise aValueErrorwhen there are duplicates columns in id_vars and not invalue_vars, as discussed and moved it to the beginning of the melt function.

I considered using theframes.columns.is_unique, but that would raise for any duplicate name - even invalue_vars which is supported. To keep the behavior consistent, I checked only for duplicate column names referenced inid_vars. Let me know if further adjustment is needed!

@@ -173,6 +173,9 @@ def melt(
1 b B E 3
2 c B E 5
"""
# GH61475 - prevent AttributeError when duplicate column in id_vars
if id_vars and any(frame.columns.tolist().count(col) > 1 for col in id_vars):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

  1. Can you move this after theid_vars = ensure_list_vars(id_vars, "id_vars", frame.columns) line
  2. Specify this asif id_vars and not frame.columns.is_unique and any(id_var in frame.column for id_var in id_vars):

Copy link
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

If I addednot frame.columns.is_unique at the beginning of the function, wouldn't it causetest_melt_with_duplicate_columns to fail, since that test currently allows duplicate column names invalue_vars?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

I'm not sure. You'll need to run the test to find out

Copy link
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

I added not frame.columns.is_unique at and it causedtest_melt_with_duplicate_columnsto fail since that test currently allows duplicate column names invalue_vars, so we might need to handle duplicates differently since we allow certain duplicates.

Copy link
Member

@mroeschkemroeschkeMay 29, 2025
edited
Loading

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Ah ok you'll need to check that theid_var values is actually a duplicate in the columns.

You can uselen(frame.columns.get_indexer_for(id_vars)) > len(id_vars)

@mroeschkemroeschke added this to the3.0 milestoneMay 30, 2025
@mroeschkemroeschke merged commit8981a0f intopandas-dev:mainMay 30, 2025
42 of 44 checks passed
@mroeschke
Copy link
Member

Thanks@ZanirP

@ZanirPZanirP deleted the melt_error_duplicate_columns branchMay 30, 2025 16:49
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@mroeschkemroeschkemroeschke approved these changes

Assignees
No one assigned
Labels
Error ReportingIncorrect or improved errors from pandasReshapingConcat, Merge/Join, Stack/Unstack, Explode
Projects
None yet
Milestone
3.0
Development

Successfully merging this pull request may close these issues.

BUG: More Indicative Error when pd.melt with duplicate columns
2 participants
@ZanirP@mroeschke

[8]ページ先頭

©2009-2025 Movatter.jp