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 all 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
4 changes: 4 additions & 0 deletions pandas/core/reshape/melt.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,10 @@ def melt(
value_vars_was_not_none = value_vars is not None
value_vars = ensure_list_vars(value_vars, "value_vars", frame.columns)

# GH61475 - prevent AttributeError when duplicate column in id_vars
if len(frame.columns.get_indexer_for(id_vars)) > len(id_vars):
raise ValueError("id_vars cannot contain duplicate columns.")

if id_vars or value_vars:
if col_level is not None:
level = frame.columns.get_level_values(col_level)
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