Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
5 changes: 5 additions & 0 deletions pandas/core/reshape/melt.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ def melt(
1 b B E 3
2 c B E 5
"""

if value_name in frame.columns:
raise ValueError(
f"value_name ({value_name}) cannot match an element in "
Expand All @@ -182,6 +183,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