Skip to content

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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 30, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v3.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -847,6 +847,7 @@ Reshaping
- Bug in :meth:`DataFrame.stack` with the new implementation where ``ValueError`` is raised when ``level=[]`` (:issue:`60740`)
- Bug in :meth:`DataFrame.unstack` producing incorrect results when manipulating empty :class:`DataFrame` with an :class:`ExtentionDtype` (:issue:`59123`)
- Bug in :meth:`concat` where concatenating DataFrame and Series with ``ignore_index = True`` drops the series name (:issue:`60723`, :issue:`56257`)
- Bug in :func:`melt` where calling with duplicate column names in ``id_vars`` raised a misleading ``AttributeError`` (:issue:`61475`)

Sparse
^^^^^^
Expand Down
3 changes: 3 additions & 0 deletions pandas/core/reshape/melt.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 the id_vars = ensure_list_vars(id_vars, "id_vars", frame.columns) line
  2. Specify this as if id_vars and not frame.columns.is_unique and any(id_var in frame.column for id_var in id_vars):

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If I added not frame.columns.is_unique at the beginning of the function, wouldn't it cause test_melt_with_duplicate_columns to fail, since that test currently allows duplicate column names in value_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
Contributor Author

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 caused test_melt_with_duplicate_columns to fail since that test currently allows duplicate column names in value_vars, so we might need to handle duplicates differently since we allow certain duplicates.

Copy link
Member

@mroeschke mroeschke May 29, 2025

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 the id_var values is actually a duplicate in the columns.

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

raise ValueError("id_vars cannot contain duplicate columns.")
if value_name in frame.columns:
raise ValueError(
f"value_name ({value_name}) cannot match an element in "
Expand Down
8 changes: 8 additions & 0 deletions pandas/tests/reshape/test_melt.py
Original file line number Diff line number Diff line change
Expand Up @@ -555,6 +555,14 @@ def test_melt_multiindex_columns_var_name_too_many(self):
):
df.melt(var_name=["first", "second", "third"])

def test_melt_duplicate_column_header_raises(self):
# GH61475
df = DataFrame([[1, 2, 3], [3, 4, 5]], columns=["A", "A", "B"])
msg = "id_vars cannot contain duplicate columns."

with pytest.raises(ValueError, match=msg):
df.melt(id_vars=["A"], value_vars=["B"])


class TestLreshape:
def test_pairs(self):
Expand Down
Loading