Skip to content

BUG: different behavior of stack(future_stack=True) and stack(future_stack=False) #54948

Closed
@radi0sus

Description

@radi0sus

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 
import numpy as np

c_1 = 'A0', 'B0', 0.11
c_2 = 'A1', 'B1', 0.12
c_3 = 'A2', 'B2', 0.14
df=pd.DataFrame(np.random.rand(3,3), columns= [c_1,c_2,c_3], index = ['(i1, i2, i3)','(i4, i5, i6)','(i7, i8, i9)'])
print(df)
#okay:
print(df.stack(future_stack=False))
print(df.unstack())
#error:
print(df.stack(future_stack=True))

Issue Description

Reporting a different behavior of stack() if future_stack=True. There is a
KeyError: "None of [Index(['A0'], dtype='object')] are in the [columns]" in case of future_stack=True.

Expected Behavior

Expected behavior ist the output from the following part of the above example:

.....
#okay:
print(df.stack(future_stack=False))
.....

Installed Versions

INSTALLED VERSIONS

commit : ba1cccd
python : 3.11.5.final.0
python-bits : 64
OS : Darwin
OS-release : 22.6.0
Version : Darwin Kernel Version 22.6.0: Wed Jul 5 22:22:52 PDT 2023; root:xnu-8796.141.3~6/RELEASE_ARM64_T8103
machine : arm64
processor : arm
byteorder : little
LC_ALL : None
LANG : de_DE.UTF-8
LOCALE : de_DE.UTF-8

pandas : 2.1.0
numpy : 1.25.2
pytz : 2023.3
dateutil : 2.8.2
setuptools : 68.1.2
pip : 23.2.1
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
bs4 : None
bottleneck : None
dataframe-api-compat: None
fastparquet : None
fsspec : None
gcsfs : None
matplotlib : 3.7.2
numba : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : 1.11.2
sqlalchemy : None
tables : None
tabulate : 0.9.0
xarray : None
xlrd : None
zstandard : None
tzdata : 2023.3
qtpy : None
pyqt5 : None

Metadata

Metadata

Assignees

Labels

BugReshapingConcat, Merge/Join, Stack/Unstack, Explode

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions