You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have checked that this issue has not already been reported.
I have confirmed this bug exists on the latest version of pandas.
(optional) I have confirmed this bug exists on the master branch of pandas.
Code Sample, a copy-pastable example
importpandasaspddf=pd.DataFrame(
[['p', 'q', 'r'],
['s', 't', 'u'],
['v', 'w', 'x'],
],
index=pd.MultiIndex.from_arrays(
[list('123'), list('456')],
names=['ind1', 'ind2']
),
columns=pd.MultiIndex.from_arrays(
[list('ABC'), list('DEF')])
)
# melt using level1 (or above in other cases) failsdf_l1=df.reset_index(col_level=1)
print("\nL1 index insert:\n", df_l1)
# NOTE: THIS FAILS!df_l1=pd.melt(df_l1, col_level=1,
id_vars=['ind1'], value_vars=['D','E'])
print("\nL1 melt:\n", df_l1)
Problem description
Suppose that we have multi-index columns and we would like to melt, using the index as the id_vars:
In this example, if we reset_index(col_level=1) and then melt() will fail as shown below:
@TomAugspurger no worries, updated the issue.
I tried a few things. what you are saying (adding columns as tuples) is only correct if I don't specify the col_level in melt. When I specify it, as shown in the API Reference, it should work by the level name (not tuples). Now, the example in the API reference uses col_level=0 which works fine, but my example uses col_level=1 which fails
I have checked that this issue has not already been reported.
I have confirmed this bug exists on the latest version of pandas.
(optional) I have confirmed this bug exists on the master branch of pandas.
Code Sample, a copy-pastable example
Problem description
Suppose that we have multi-index columns and we would like to
melt
, using the index as theid_vars
:In this example, if we
reset_index(col_level=1)
and thenmelt()
will fail as shown below:Output of
pd.show_versions()
INSTALLED VERSIONS
commit : None
python : 3.8.0.final.0
python-bits : 64
OS : Linux
OS-release : 5.3.0-51-generic
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : en_GB.UTF-8
LOCALE : en_GB.UTF-8
pandas : 1.0.3
numpy : 1.18.4
pytz : 2020.1
dateutil : 2.8.1
pip : 20.1
setuptools : 40.8.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 : 7.14.0
pandas_datareader: None
bs4 : None
bottleneck : None
fastparquet : None
gcsfs : None
lxml.etree : None
matplotlib : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pytables : None
pytest : None
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
xlsxwriter : None
numba : None
The text was updated successfully, but these errors were encountered: