Join GitHub today
GitHub is home to over 28 million developers working together to host and review code, manage projects, and build software together.Sign up
sort_index with sort_remaining=False does not work with int level #21052
Code Sample, a copy-pastable example if possible
import pandas as pd mi = pd.MultiIndex.from_tuples([[2,2],[2,1],[1,2],[1,1]], names=list('AB')) a = pd.Series([1,2,3,4], index=mi) a.sort_index(level=0, sort_remaining=False)
Besides A (level 0), also B (level 1) is sorted. This, despite the fact that sort_remaining is set to False.
By comparison, the correct result is obtained when specifying the level by its name:
Exactly the same result should be expected when using
Thanks for the bug. This is probably related to #21043, though the work done there does not appear to solve it.
Interestingly enough, the difference in sorting is ONLY observed at the first level. If your items were say at the second level there would be no difference.
In : mi = pd.MultiIndex.from_tuples([[0,2,2],[0,2,1],[0,1,2],[0,1,1]], names=list('CAB')) In : a = pd.Series([1,2,3,4], index=mi) In : a.sort_index(level=1, sort_remaining=False) Out: C A B 0 1 2 3 1 4 2 2 1 1 2 dtype: int64 In : a.sort_index(level='A', sort_remaining=False) Out: C A B 0 1 2 3 1 4 2 2 1 1 2 dtype: int64
Thus, the problem occurs only when level=0. Hmm, that sounds familiar. Indeed, a simple
In other words, level=0 is handled in the same way as level=None, which means that all levels are sorted...
The solution seems simple: