Skip to content

BUG: set_index doesn't work with MI. #5358

Closed
@jtratner

Description

@jtratner

Changes it to tuples instead (so it's probably just converting to list or going to values somewhere)

In [9]: df = DataFrame(list(zip(range(3), range(3), range(3), range(3))))

In [10]: ind = MultiIndex.from_arrays([['x', 'y', 'z'], [4, 4, 5]], names=['ind1', 'ind2'])

In [11]: df.set_index(ind)
Out[11]:
        0  1  2  3
(x, 4)  0  0  0  0
(y, 4)  1  1  1  1
(z, 5)  2  2  2  2
In [12]: df.reindex(ind) # works
Out[12]:
            0   1   2   3
ind1 ind2
x    4    NaN NaN NaN NaN
y    4    NaN NaN NaN NaN
z    5    NaN NaN NaN NaN
In [13]: df2 = df.copy()

In [14]: df2.index = ind # works too

In [15]: df2
Out[15]:
           0  1  2  3
ind1 ind2
x    4     0  0  0  0
y    4     1  1  1  1
z    5     2  2  2  2

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions