Skip to content
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

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

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion pandas/core/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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,))

Expand Down