Skip to content

Commit 53e39f7

Browse files
ZanirPZanirP
ZanirP
authored and
ZanirP
committed
BUG: Raise clear error for duplicate id_vars in melt (GH61475)
1 parent cfe54bd commit 53e39f7

File tree

3 files changed

+12
-0
lines changed

3 files changed

+12
-0
lines changed

doc/source/whatsnew/v3.0.0.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -847,6 +847,7 @@ Reshaping
847847
- Bug in :meth:`DataFrame.stack` with the new implementation where ``ValueError`` is raised when ``level=[]`` (:issue:`60740`)
848848
- Bug in :meth:`DataFrame.unstack` producing incorrect results when manipulating empty :class:`DataFrame` with an :class:`ExtentionDtype` (:issue:`59123`)
849849
- Bug in :meth:`concat` where concatenating DataFrame and Series with ``ignore_index = True`` drops the series name (:issue:`60723`, :issue:`56257`)
850+
- Bug in :func:`melt` where calling with duplicate column names in ``id_vars`` raised a misleading ``AttributeError`` (:issue:`61475`)
850851

851852
Sparse
852853
^^^^^^

pandas/core/reshape/melt.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,9 @@ def melt(
239239
mdata: dict[Hashable, AnyArrayLike] = {}
240240
for col in id_vars:
241241
id_data = frame.pop(col)
242+
# GH61475 - prevent AttributeError when duplicate column
243+
if not hasattr(id_data, "dtype"):
244+
raise Exception(f"{col} is a duplicate column header")
242245
if not isinstance(id_data.dtype, np.dtype):
243246
# i.e. ExtensionDtype
244247
if num_cols_adjusted > 0:

pandas/tests/reshape/test_melt.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -555,6 +555,14 @@ def test_melt_multiindex_columns_var_name_too_many(self):
555555
):
556556
df.melt(var_name=["first", "second", "third"])
557557

558+
def test_melt_duplicate_column_header_raises(self):
559+
# GH61475
560+
df = DataFrame([[1, 2, 3], [3, 4, 5]], columns=["A", "A", "B"])
561+
msg = "A is a duplicate column header"
562+
563+
with pytest.raises(Exception, match=msg):
564+
df.melt(id_vars=["A"], value_vars=["B"])
565+
558566

559567
class TestLreshape:
560568
def test_pairs(self):

0 commit comments

Comments
 (0)