Join GitHub today
GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together.Sign up
Clarify that MultiIndex.set_levels() interprets passed values as new components of the .levels attribute #28294
Code Sample, a copy-pastable example if possible
import numpy as np import pandas as pd df = pd.DataFrame(np.random.rand(3, 3), columns=pd.MultiIndex.from_tuples([(1, 2), (1, 4), (5, 6)])) df.columns = df.columns.set_levels([1, 1, 3], level=0)
Which raises a
ValueError: Level values must be unique: [1, 1, 3] on level 0
Despite a dataframe with non-unique MultiIndex can be created, they cannot be set using
1 3 2 4 6 0 0.317669 0.329142 0.056725 1 0.969472 0.340309 0.135204 2 0.242408 0.934748 0.683186
Those issues were about non-unique level names, yours about non-unique level values. And in this second case the story is simple: yes, they are and always were allowed.
But I think you've misunderstood how
In short, this function is probably not what you're looking for. And unfortunately, there are no obvious alternative to do what you want to do.