Skip to content

Commit 9ac9699

Browse files
Backport PR #41964: Bug in melt raising Error with dup columns as value vars (#42035)
Co-authored-by: Patrick Hoefler <[email protected]>
1 parent c4afef8 commit 9ac9699

File tree

3 files changed

+12
-1
lines changed

3 files changed

+12
-1
lines changed

doc/source/whatsnew/v1.3.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -1155,6 +1155,7 @@ Reshaping
11551155
- Bug in :func:`to_datetime` raising an error when the input sequence contained unhashable items (:issue:`39756`)
11561156
- Bug in :meth:`Series.explode` preserving the index when ``ignore_index`` was ``True`` and values were scalars (:issue:`40487`)
11571157
- Bug in :func:`to_datetime` raising a ``ValueError`` when :class:`Series` contains ``None`` and ``NaT`` and has more than 50 elements (:issue:`39882`)
1158+
- Bug in :meth:`DataFrame.melt` raising ``InvalidIndexError`` when :class:`DataFrame` has duplicate columns used as ``value_vars`` (:issue:`41951`)
11581159

11591160
Sparse
11601161
^^^^^^

pandas/core/reshape/melt.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
from pandas.core.dtypes.concat import concat_compat
2222
from pandas.core.dtypes.missing import notna
2323

24+
import pandas.core.algorithms as algos
2425
from pandas.core.arrays import Categorical
2526
import pandas.core.common as com
2627
from pandas.core.indexes.api import (
@@ -106,7 +107,7 @@ def melt(
106107
id_vars + value_vars
107108
)
108109
else:
109-
idx = frame.columns.get_indexer(id_vars + value_vars)
110+
idx = algos.unique(frame.columns.get_indexer_for(id_vars + value_vars))
110111
frame = frame.iloc[:, idx]
111112
else:
112113
frame = frame.copy()

pandas/tests/reshape/test_melt.py

+9
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,15 @@ def test_ignore_index_name_and_type(self):
403403

404404
tm.assert_frame_equal(result, expected)
405405

406+
def test_melt_with_duplicate_columns(self):
407+
# GH#41951
408+
df = DataFrame([["id", 2, 3]], columns=["a", "b", "b"])
409+
result = df.melt(id_vars=["a"], value_vars=["b"])
410+
expected = DataFrame(
411+
[["id", "b", 2], ["id", "b", 3]], columns=["a", "variable", "value"]
412+
)
413+
tm.assert_frame_equal(result, expected)
414+
406415

407416
class TestLreshape:
408417
def test_pairs(self):

0 commit comments

Comments
 (0)