Description
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