Skip to content
Open
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.1.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,7 @@ Groupby/resample/rolling
Reshaping
^^^^^^^^^
- Bug in :func:`merge` where merging on a :class:`MultiIndex` containing ``NaN`` values mapped ``NaN`` keys to the last level value instead of ``NaN`` (:issue:`64492`)
- Bug in :meth:`DataFrame.melt` where ``var_name`` colliding with an ``id_vars`` column or ``value_name`` silently overwrote the affected column data instead of raising (:issue:`65654`)
- Bug in :meth:`DataFrame.pivot_table` with ``margins=True`` raising ``TypeError`` when ``values`` has an :class:`ExtensionDtype` that cannot hold ``NA`` (e.g. :class:`IntervalDtype` with an integer subtype) and no ``columns`` were specified (:issue:`55484`)
- Bug in :meth:`Index.union` where the result could be unsorted when both inputs were monotonic increasing but disjoint, when ``sort`` was not ``False`` (:issue:`54646`)
- In :func:`pivot_table`, when ``values`` is empty, the aggregation will be computed on a Series of all NA values (:issue:`46475`)
Expand Down
15 changes: 15 additions & 0 deletions pandas/core/reshape/melt.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,21 @@ def melt(
else:
var_name = [var_name]

output_names = (*id_vars, *var_name, value_name)
if len(set(output_names)) != len(output_names):
seen = set()
dups = []
for n in output_names:
if n in seen:
dups.append(n)
else:
seen.add(n)
raise ValueError(
f"melt would produce duplicate column names {dups} from "
f"id_vars={id_vars!r}, var_name={var_name!r}, "
f"value_name={value_name!r}."
)

num_rows, K = frame.shape
num_cols_adjusted = K - len(id_vars)

Expand Down
16 changes: 16 additions & 0 deletions pandas/tests/reshape/test_melt.py
Original file line number Diff line number Diff line change
Expand Up @@ -563,6 +563,22 @@ def test_melt_duplicate_column_header_raises(self):
with pytest.raises(ValueError, match=msg):
df.melt(id_vars=["A"], value_vars=["B"])

@pytest.mark.parametrize(
"kwargs",
[
{"id_vars": "id", "var_name": "id"},
{"id_vars": "id", "var_name": "x", "value_name": "x"},
{"id_vars": ["id", "variable"], "value_vars": ["a"]},
],
)
def test_melt_var_name_collision_raises(self, kwargs):
# GH 65654
df = DataFrame(
{"id": [1, 2], "variable": [9, 9], "a": [10, 20], "b": [100, 200]}
)
with pytest.raises(ValueError, match="duplicate column names"):
df.melt(**kwargs)


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