Skip to content

BUG: No longer can melt name the columns for the labels of a MultiIndex #58033

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
3 tasks done
ghost opened this issue Mar 27, 2024 · 1 comment · Fixed by #58088
Closed
3 tasks done

BUG: No longer can melt name the columns for the labels of a MultiIndex #58033

ghost opened this issue Mar 27, 2024 · 1 comment · Fixed by #58088
Assignees
Labels
Bug Needs Triage Issue that has not been reviewed by a pandas team member

Comments

@ghost
Copy link

ghost commented Mar 27, 2024

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

frame = pd.DataFrame({("A", "a"): range(3), ("A", "b"): range(3, 6)})
frame.melt(var_name=["upper", "lower"])

Issue Description

Running the example gives

Traceback (most recent call last):
  File "<string>", line 19, in __PYTHON_EL_eval
  File "/home/jdawson/Documents/bugs/pandas-melt/melt_bug.py", line 5, in <module>
    frame.melt(var_name=["upper", "lower"])
  File "/home/jdawson/Documents/bugs/pandas-melt/.venv/lib/python3.12/site-packages/pandas/core/frame.py", line 9839, in melt
    return melt(
           ^^^^^
  File "/home/jdawson/Documents/bugs/pandas-melt/.venv/lib/python3.12/site-packages/pandas/core/reshape/melt.py", line 220, in melt
    raise ValueError(f"{var_name=} must be a scalar.")
ValueError: var_name=['upper', 'lower'] must be a scalar.

We can melt the frame if we do not supply var_name, but the columns for the labels of the MultiIndex will have the abstruse names variable_0 and variable_1.

>>> frame.melt()
  variable_0 variable_1  value
0          A          a      0
1          A          a      1
2          A          a      2
3          A          b      3
4          A          b      4
5          A          b      5

This bug may be related to #57663.

Expected Behavior

Versions strictly older than 2.2.0 behave thus.

>>> import pandas as pd
>>> frame = pd.DataFrame({("A", "a"): range(3), ("A", "b"): range(3, 6)})
>>> frame
   A   
   a  b
0  0  3
1  1  4
2  2  5
>>> frame.melt(var_name=["upper", "lower"])
  upper lower  value
0     A     a      0
1     A     a      1
2     A     a      2
3     A     b      3
4     A     b      4
5     A     b      5

Installed Versions

INSTALLED VERSIONS

commit : b552dc9
python : 3.12.2.final.0
python-bits : 64
OS : Linux
OS-release : 5.15.0-101-generic
Version : #111~20.04.1-Ubuntu SMP Mon Mar 11 15:44:43 UTC 2024
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : en_GB.UTF-8
LOCALE : en_GB.UTF-8

pandas : 3.0.0.dev0+618.gb552dc95c
numpy : 1.26.4
pytz : 2024.1
dateutil : 2.9.0.post0
setuptools : None
pip : 24.0
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : None
IPython : None
pandas_datareader : None
adbc-driver-postgresql: None
adbc-driver-sqlite : None
bs4 : None
bottleneck : None
fastparquet : None
fsspec : None
gcsfs : None
matplotlib : None
numba : None
numexpr : None
odfpy : None
openpyxl : None
pyarrow : None
pyreadstat : None
python-calamine : None
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
zstandard : None
tzdata : 2024.1
qtpy : None
pyqt5 : None

@ghost ghost added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Mar 27, 2024
@rob-sil
Copy link
Contributor

rob-sil commented Mar 30, 2024

take

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Needs Triage Issue that has not been reviewed by a pandas team member
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant