Skip to content

DataFrame.unstack() issue when unstacking multiple levels. #451

Closed
@lodagro

Description

@lodagro

Let`s create a dataframe first.

In [170]: import pandas

In [171]: import numpy as np

In [172]: import itertools

In [173]: index = pandas.MultiIndex.from_tuples([t for t in itertools.product(['foo', 'bar'], [0, 1] , ['a', 'b'])])

In [174]: df = pandas.DataFrame(np.random.randn(8,2), columns=['A', 'B'], index=index)

In [175]: df
Out[175]:
         A         B
foo 0 a -1.627     1.301
      b -494.049m  120.371m
foo 1 a  955.212m  580.450m
      b -55.806m   169.247m
bar 0 a  266.666m  246.100m
      b  806.406m  894.707m
bar 1 a -1.319     475.675m
      b  638.371m  409.483m

Unstacking a single level works fine.

In [176]: df.unstack(level=[1])
Out[176]:
       A                   B
       0         1         0         1
bar a  266.666m -1.319     246.100m  475.675m
    b  806.406m  638.371m  894.707m  409.483m
foo a -1.627     955.212m  1.301     580.450m
    b -494.049m -55.806m   120.371m  169.247m

Trying to unstack multiple level does not work as expected.
Looks like when handling multiple levels the unstack are applied consecutively, ok for the first unstack, but following ones should have level adapted internally, which does not seem to happen.
First example tries to unstack level 0 and 1 but level 0 and 2 get unstacked.
Second example, idea is to unstack level 1 and 2 but this gives an error, since after level 1 is unstacked, there is no level 2 any more this became level 1 - see also last example.

In [180]: df.unstack(level=[0,1])
Out[180]:
   A                   A                   B                   B
   bar                 foo                 bar                 foo
   a         b         a         b         a         b         a         b
0  266.666m  806.406m -1.627    -494.049m  246.100m  894.707m  1.301     120.371m
1 -1.319     638.371m  955.212m -55.806m   475.675m  409.483m  580.450m  169.247m


In [177]: df.unstack(level=[1,2])
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
...
IndexError: pop index out of range

In [178]: df.unstack(level=[1,1])
Out[178]:
     A                   A                   B                   B
     0                   1                   0                   1
     a         b         a         b         a         b         a         b
bar  266.666m  806.406m -1.319     638.371m  246.100m  894.707m  475.675m  409.483m
foo -1.627    -494.049m  955.212m -55.806m   1.301     120.371m  580.450m  169.247m

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions