Skip to content

Multiple unstack in multi level columns DataFrame #24729

Closed
@faulaire

Description

@faulaire

Code Sample, a copy-pastable example if possible

import pandas as pd
from six import StringIO
stats = pd.DataFrame({('B', 'C'): 
                          {
                              (10, 20, 30): 0.0,
                              (10, 20, 40): 0.0
                          },
                      ('B', 'D'): 
                          {
                              (10, 20, 30): 0.0, 
                              (10, 20, 40): 0.0
                          }
                     })

stats.index.names = ['i1', 'i2', 'i3']
stats.columns.names = ['c1', 'c2']

# Expected true,  actual false
stats.unstack(['i2', 'i1']).columns.names[-2:] == ['i2', 'i1']

# In case of single level column, expected true, actual true
stats['B'].unstack(['i2', 'i1']).columns.names[-2:] == ['i2', 'i1']

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    MultiIndexReshapingConcat, Merge/Join, Stack/Unstack, Explode

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions