Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

ENH: Allow to sort on index level by name #141

Closed
wants to merge 2 commits into
from
Jump to file or symbol
Failed to load files and symbols.
+9 −1
Split
View
@@ -790,7 +790,8 @@ def sortlevel(self, level=0, ascending=True):
Parameters
----------
- level : int, default 0
+ level : int or str, default 0
+ If a string is given, must be a name of the level
ascending : boolean, default True
False to sort in descending order
@@ -799,8 +800,15 @@ def sortlevel(self, level=0, ascending=True):
sorted_index : MultiIndex
"""
labels = list(self.labels)
+ if not isinstance(level, int):
+ try:
+ level = self.names.index(level)
+ except:
+ raise ValueError("level %s not in index names" % level)
+
primary = labels.pop(level)
+
# Lexsort starts from END
indexer = np.lexsort(tuple(labels[::-1]) + (primary,))