-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
Indexing a multi-index does not respect order of list indexer #10710
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
This line here calls the union() on the indexer object. The union() method in turn sorts the returned values. Therefore, we end up with array([0,1,2,3]), whereas what we need is array([2,3,0,1]) One option would be to, gather the values from union() and re-arrange after this line to align with the provided order of keys. |
One subtlety I hadn't considered:
The current behavior is not, strictly speaking, buggy: it just follows the first interpretation. And this is not surprising... as the second currently doesn't work! I do think that users tend to expect the second. Moreover, if any user were expect the first, the second still wouldn't be too annoying (just a matter of reordering the keys passed). But first... we should make (In progress) |
This was actually rather misleading for me, also considering pandas behaviour with single indexed columns. There, you can select a shuffled view by passing in a list with the correct order, i.e. In my case, I tried to combine this knowledge with the multi index, expecting the first use case as mentioned by @toobaz import pandas as pd
import numpy as np
idx = pd.IndexSlice
cols = pd.MultiIndex.from_product([['A', 'B', 'C'],[1,2]])
df = pd.DataFrame(np.random.randn(5,6), columns=cols)
df.loc[:, idx[["C", "A"], 1]]
A C
1 1
0 -1.496248 0.486082
1 1.745915 0.478067
2 0.024262 -0.494881
3 -0.151359 0.148930
4 1.205520 0.374346 and I was bitten rather badly by it, for obvious reasons |
I found this thread, because I too would like to specify an ordering and have it reflected in the result (interpretation 2 from @toobaz ). |
Order seems to be respected now on master. Could use a test.
|
I have tested this with pandas version 1.2.4 and I can confirm that it does indeed work. |
The text was updated successfully, but these errors were encountered: