Skip to content

BUG in _unstack_multiple #32624

@r7sa

Description

@r7sa

Code Sample

Next code fail to do unstack.

d = pd.DataFrame([[1,2,3],[2,2,3],[3,2,3],[4,2,3],[5,2,3]], 
                 columns=pd.MultiIndex.from_tuples([[0, 0, 0], [0, 0, 1], [0, 0, 2]], names=['c1', 'c2', 'c3']),
                 index=pd.MultiIndex.from_tuples([[0, 0, 0, 0, 0, 0, 0],
                                                  [0, 0, 1, 0, 0, 0, 1],
                                                  [0, 1, 0, 0, 0, 1, 0],
                                                  [0, 1, 1, 0, 0, 1, 1],
                                                  [1, 0, 0, 0, 1, 0, 0]
                                                  ], 
                                                 names=['i1', 'i2', 'i3', 'i4', 'i5', 'i6', 'i7']))
e = d.unstack(['i2', 'i3', 'i4', 'i5', 'i6', 'i7'])

It rise exception IndexError with message "Too many levels: Index has only 2 levels, not 3"

Problem description

The reason semms to mistyping in https://github.com/pandas-dev/pandas/blob/master/pandas/core/reshape/reshape.py#L366 :

clocs = [v if i > v else v - 1 for v in clocs]

I think it must be:

clocs = [v if val > v else v - 1 for v in clocs]

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