Pandas version checks
-
[X] I have checked that this issue has not already been reported.
-
[X] I have confirmed this bug exists on the latest version of pandas.
-
[X] I have confirmed this bug exists on the main branch of pandas.
Reproducible Example
import pandas as pd
ind= pd.MultiIndex.from_arrays([['B', 'C', 'A', 'A'],
['H', 'G', 'F', 'G'],
['V', 'U', 'W', 'U']])
ind = ind.set_levels(['B','M','A'], level=0)
ind.sortlevel(0, sort_remaining=True)
Issue Description
Result is not sorted:
(MultiIndex([('B', 'F', 'W'),
('B', 'G', 'U'),
('M', 'H', 'V'),
('A', 'G', 'U')],
),
array([2, 3, 0, 1]))
Expected Behavior
MultiIndex should be sorted. Cryptic phrase in documentation: "The result will respect the original ordering of the associated factor at that level." is not productive. A function called sortlevel
should not depend on some mysterious "original ordering of an associated factor at that level".
If anything, please rename the function to: sortlevel_based_on_original_ordering_of_the_associated_factor_at_that_level
, otherwise people will be confused that it's not working. Also, what the "original ordering of the associated factor at that level" is, is nowhere discussed. Also, for consistency reasons, the function should be called: sort_level
(or sort_levels
?) not sortlevel
.