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
The text was updated successfully, but these errors were encountered:
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: