Skip to content

BUG: melt no longer supports id_vars and value_vars collapsing levels accross MultiIndex #57663

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

Open
3 tasks done
mark-thm opened this issue Feb 28, 2024 · 2 comments
Open
3 tasks done
Labels
Bug Regression Functionality that used to work in a prior pandas version Reshaping Concat, Merge/Join, Stack/Unstack, Explode
Milestone

Comments

@mark-thm
Copy link

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

period1 = pd.Period("2018Q1")
df = pd.DataFrame(
    [[1.0]],
    index=pd.PeriodIndex([period1, period1], name="period"),
    columns=pd.MultiIndex.from_tuples(
        [("a", "b")],
        names=["x", "y"],
    ),
)

pd.melt(df, id_vars=["a"])

Issue Description

Starting in 2.2.0, pd.melt claims that columns referenced in id_vars or value_vars are not present in a MultiIndex:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "venv/lib/python3.11/site-packages/pandas/core/reshape/melt.py", line 74, in melt
    raise KeyError(
KeyError: "The following id_vars or value_vars are not present in the DataFrame: ['a']"

Expected Behavior

Prior to 2.2.0, pd.melt melts the dataframe.

It appears the behavior was changed in this commit: https://github.com/pandas-dev/pandas/pull/55948/files#diff-a851c9e0a30604de55d051b077b0d9d2ab92d2386863991a2a4cb4444f103bffL44-L50

Installed Versions

INSTALLED VERSIONS

commit : fd3f571
python : 3.11.7.final.0
python-bits : 64
OS : Darwin
OS-release : 23.3.0
Version : Darwin Kernel Version 23.3.0: Wed Dec 20 21:30:44 PST 2023; root:xnu-10002.81.5~7/RELEASE_ARM64_T6000
machine : x86_64
processor : i386
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

pandas : 2.2.0
numpy : 1.26.4
pytz : 2024.1
dateutil : 2.8.2
setuptools : 69.1.0
pip : 23.2.1
Cython : None
pytest : 8.0.1
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : 5.1.0
html5lib : 1.1
pymysql : 1.4.6
psycopg2 : None
jinja2 : 3.1.3
IPython : 8.9.0
pandas_datareader : None
adbc-driver-postgresql: None
adbc-driver-sqlite : None
bs4 : 4.12.3
bottleneck : 1.3.7
dataframe-api-compat : None
fastparquet : None
fsspec : 2024.2.0
gcsfs : 2024.2.0
matplotlib : 3.8.3
numba : 0.59.0
numexpr : 2.9.0
odfpy : None
openpyxl : 3.1.2
pandas_gbq : None
pyarrow : 15.0.0
pyreadstat : None
python-calamine : None
pyxlsb : 1.0.10
s3fs : 2024.2.0
scipy : 1.12.0
sqlalchemy : None
tables : None
tabulate : None
xarray : 2024.2.0
xlrd : None
zstandard : None
tzdata : 2024.1
qtpy : None
pyqt5 : None

@mark-thm mark-thm added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Feb 28, 2024
@phofl
Copy link
Member

phofl commented Mar 18, 2024

cc @mroeschke

@phofl phofl added this to the 2.2.2 milestone Mar 18, 2024
@phofl phofl added Reshaping Concat, Merge/Join, Stack/Unstack, Explode Regression Functionality that used to work in a prior pandas version and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Mar 18, 2024
@lithomas1 lithomas1 modified the milestones: 2.2.2, 2.2.3 Apr 10, 2024
@lithomas1 lithomas1 modified the milestones: 2.2.3, 2.3 Sep 21, 2024
@Bassix9
Copy link

Bassix9 commented Apr 8, 2025

I kindly want to ask when this issue will be solved.

My working setup: Python 3.11.9 and pandas==2.0.3
Failing on Python 3.13.2 and pandas==2.2.3

The "Reproducible Example" doesn't work for me at all, but the error message of my app is the same. Thus, I assume the same root cause.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Regression Functionality that used to work in a prior pandas version Reshaping Concat, Merge/Join, Stack/Unstack, Explode
Projects
None yet
Development

No branches or pull requests

4 participants