Skip to content

BUG: improve error message for duplicate columns in pd.melt (GH61475)#65189

Open
mriganshu16 wants to merge 2 commits intopandas-dev:mainfrom
mriganshu16:fix/melt-duplicate-columns-error
Open

BUG: improve error message for duplicate columns in pd.melt (GH61475)#65189
mriganshu16 wants to merge 2 commits intopandas-dev:mainfrom
mriganshu16:fix/melt-duplicate-columns-error

Conversation

@mriganshu16
Copy link
Copy Markdown

Closes #61475

Problem Description

When calling pd.melt() on a DataFrame with duplicate column names,
the previous error message was generic and didn't identify which column
caused the problem:

x = pd.DataFrame([[1, 2, 3], [3, 4, 5]], columns=["A", "A", "B"])
pd.melt(x, id_vars=["A"], value_vars=["B"])
# Before: ValueError: id_vars cannot contain duplicate columns.
# After:  ValueError: id_vars contains columns with duplicate labels
#         in the DataFrame: ['A']. Please rename these columns before melting.

Changes

  • pandas/core/reshape/melt.py: replaced the generic check with one
    that identifies and names the specific duplicate column(s)
  • pandas/tests/reshape/test_melt.py: updated existing test to match
    new message; added a second test to verify the column name appears
    in the error

@mriganshu16 mriganshu16 force-pushed the fix/melt-duplicate-columns-error branch from 87396f4 to 241136b Compare April 12, 2026 09:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

BUG: More Indicative Error when pd.melt with duplicate columns

2 participants