Skip to content

BUG: DataFrame.melt silently corrupts data on var_name output-name collisions #65654

Description

@gautamvarmadatla

Pandas version checks

  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of pandas.

  • I have confirmed this bug exists on the main branch of pandas.

Reproducible Example

import pandas as pd
df = pd.DataFrame({"id": [1, 2], "a": [10, 20], "b": [100, 200]})
# var_name collides with the id_vars column "id"
out = df.melt(id_vars="id", var_name="id")
print(out)

output :
 
    id id  value
  0  a  a     10
  1  a  a     20
  2  b  b    100
  3  b  b    200

Issue Description

DataFrame.melt can silently overwrite id_vars data with variable labels when the resolved var_name output column collides with an id_var name. Related output-name collisions, such as var_name colliding with value_name or duplicate entries in a list-like var_name, can also produce silently incorrect output. pandas already rejects value_name collisions with existing column labels, but analogous output-name validation is missing for var_name

Expected Behavior

melt should raise a ValueError because the requested output column names would be duplicated:

["id", "id", "value"]

The original id_vars data should not be silently overwritten.

Installed Versions

Details

INSTALLED VERSIONS

commit : 2a7587f
python : 3.12.12
python-bits : 64
OS : Windows
OS-release : 11
Version : 10.0.26200
machine : AMD64
processor : Intel64 Family 6 Model 183 Stepping 1, GenuineIntel
byteorder : little
LC_ALL : None
LANG : None
LOCALE : English_United States.1252

pandas : 3.1.0.dev0+526.g2a7587fed0.dirty
numpy : 2.4.2
dateutil : 2.9.0.post0
pip : 26.0.1
Cython : 3.2.4
sphinx : None
IPython : 9.10.0
adbc-driver-postgresql: None
adbc-driver-sqlite : None
bs4 : None
bottleneck : 1.6.0
fastparquet : None
fsspec : None
html5lib : None
hypothesis : 6.151.6
gcsfs : None
jinja2 : None
lxml.etree : None
matplotlib : 3.10.8
numba : None
numexpr : 2.14.1
odfpy : None
openpyxl : None
psycopg2 : None
pymysql : None
pyarrow : None
pyiceberg : None
pyreadstat : None
pytest : 9.0.2
python-calamine : None
pytz : 2025.2
pyxlsb : None
s3fs : None
scipy : 1.17.0
sqlalchemy : 2.0.46
tables : None
tabulate : 0.9.0
xarray : None
xlrd : None
xlsxwriter : None
zstandard : None
qtpy : None
pyqt5 : None

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugReshapingConcat, Merge/Join, Stack/Unstack, Explode

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions