Skip to content

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

Closed
faulaire opened this issue Jan 11, 2019 · 4 comments · Fixed by #32990
Closed

Multiple unstack in multi level columns DataFrame #24729

faulaire opened this issue Jan 11, 2019 · 4 comments · Fixed by #32990
Labels
MultiIndex Reshaping Concat, Merge/Join, Stack/Unstack, Explode
Milestone

Comments

@faulaire
Copy link
Contributor

faulaire commented Jan 11, 2019

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

@gfyoung gfyoung added MultiIndex Reshaping Concat, Merge/Join, Stack/Unstack, Explode labels Jan 11, 2019
@gfyoung
Copy link
Member

gfyoung commented Jan 11, 2019

cc @jreback @toobaz

@gfyoung
Copy link
Member

gfyoung commented Jan 11, 2019

@faulaire : Do you mind modifying your example slightly to construct the DataFrame explicitly instead of via read_csv ? That way, it makes it clear to everyone that we're just focusing on the (un)stacking, and that CSV functionality is not really involved in this issue.

@faulaire
Copy link
Contributor Author

@gfyoung : done.

@alexmemory
Copy link

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']

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
MultiIndex Reshaping Concat, Merge/Join, Stack/Unstack, Explode
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants