BUG in _unstack_multiple (original) (raw)
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]