-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
Multiple unstack in multi level columns DataFrame #24729
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
Comments
@faulaire : Do you mind modifying your example slightly to construct the |
@gfyoung : done. |
Here's a different example of unexpected unstack behavior that may be related. Like @faulaire's example, for single level columns the behavior is the expected one. import pandas as pd
df = (pd.DataFrame({**{'a'+str(n):[1] for n in range(7)},
**{'b0':[2]}})
.set_index(['a'+str(n) for n in range(7)]))
df = pd.concat([df], keys=['b1'], axis=1)
# Expected true, actual true
df.unstack(['a0', 'a1', 'a2']).index.names == ['a3', 'a4', 'a5', 'a6']
# Expected true, actual false
df.unstack(['a0', 'a1', 'a2', 'a3']).index.names == ['a4', 'a5', 'a6']
# Expected false, actual true
df.unstack(['a0', 'a1', 'a2', 'a3']).index.names == ['a3', 'a5', 'a6']
# Expected true, actual false
df.unstack(['a0', 'a1', 'a2', 'a3', 'a4']).index.names == ['a5', 'a6']
# Expected false, actual true
df.unstack(['a0', 'a1', 'a2', 'a3', 'a4']).index.names == ['a3', 'a6']
# If single level column, no problem
df.columns = df.columns.droplevel()
# Expected true, actual true
df.unstack(['a0', 'a1', 'a2', 'a3']).index.names == ['a4', 'a5', 'a6'] |
Code Sample, a copy-pastable example if possible
Problem description
Trying to unstack multiindex multicolumns DataFrame, yield to an unexpected behavior.
The unstacked levels are not those expected (see the code above).
Note: In a single level column case, behavior is the expected one.
Expected Output
N/A
Output of
pd.show_versions()
INSTALLED VERSIONS
commit: None
python: 2.7.13.final.0
python-bits: 64
OS: Linux
OS-release: 2.6.32-642.6.2.el6.x86_64
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: en_US.UTF-8
LANG: en_US.UTF-8
LOCALE: None.None
pandas: 0.23.2
pytest: 3.2.3
pip: 9.0.1
setuptools: 36.3.0
Cython: 0.27.3
numpy: 1.13.3
scipy: 0.19.1
pyarrow: None
xarray: None
IPython: 5.1.0
sphinx: 1.4.4
patsy: None
dateutil: 2.6.1
pytz: 2017.2
blosc: None
bottleneck: None
tables: 3.2.1
numexpr: 2.6.5
feather: None
matplotlib: 2.1.2
openpyxl: None
xlrd: None
xlwt: None
xlsxwriter: None
lxml: None
bs4: None
html5lib: 0.9999999
sqlalchemy: None
pymysql: None
psycopg2: None
jinja2: 2.8
s3fs: None
fastparquet: None
pandas_gbq: None
pandas_datareader: None
The text was updated successfully, but these errors were encountered: