Skip to content

melt does not recognize numeric column names  #29718

Closed
@nils-braun

Description

@nils-braun

Code Sample, a copy-pastable example if possible

import pandas as pd

df = pd.DataFrame(columns=[1, "string"])
pd.melt(df, id_vars=[1, "string"])

Problem description

The shown example fails with

Traceback (most recent call last):
  File "test.py", line 5, in <module>
    pd.melt(df, id_vars=[1, "string"])
  File "/home/nils/projects/tsfresh/venv/lib/python3.6/site-packages/pandas/core/reshape/melt.py", line 52, in melt
    "".format(missing=list(missing))
KeyError: "The following 'id_vars' are not present in the DataFrame: ['1']"

and I guess the reason is that the call of

Index(np.ravel(id_vars))

in pd.melt somehow casts the numerical column name 1 to the string "1".

I am not sure if this is intended behavior or if the case of numerical column names is just not supported, but at least in older pandas versions (e.g. 0.23.4) this still worked.

Thanks for looking into this! I am also fine if this is closed with "won't fix" :-)

Output of pd.show_versions()

INSTALLED VERSIONS

commit : None
python : 3.6.8.final.0
python-bits : 64
OS : Linux
OS-release : 4.15.0-65-generic
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

pandas : 0.25.3
numpy : 1.17.4
pytz : 2019.3
dateutil : 2.8.1
pip : 19.3.1
setuptools : 41.6.0
Cython : None
pytest : 5.2.4
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : 4.4.1
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 2.10.3
IPython : 7.9.0
pandas_datareader: 0.8.1
bs4 : None
bottleneck : None
fastparquet : None
gcsfs : None
lxml.etree : 4.4.1
matplotlib : 3.1.1
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pytables : None
s3fs : None
scipy : 1.3.2
sqlalchemy : None
tables : None
xarray : None
xlrd : None
xlwt : None
xlsxwriter : None

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugReshapingConcat, Merge/Join, Stack/Unstack, Explode

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions