Skip to content

DOC: fix PR07,SA01 for pandas.MultiIndex.sortlevel #59521

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

Merged
Merged
Show file tree
Hide file tree
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
1 change: 0 additions & 1 deletion ci/code_checks.sh
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ if [[ -z "$CHECK" || "$CHECK" == "docstrings" ]]; then
-i "pandas.Series.dt PR01" `# Accessors are implemented as classes, but we do not document the Parameters section` \
-i "pandas.MultiIndex.names SA01" \
-i "pandas.MultiIndex.reorder_levels RT03,SA01" \
-i "pandas.MultiIndex.sortlevel PR07,SA01" \
-i "pandas.MultiIndex.to_frame RT03" \
-i "pandas.NA SA01" \
-i "pandas.NaT SA01" \
Expand Down
22 changes: 19 additions & 3 deletions pandas/core/indexes/multi.py
Original file line number Diff line number Diff line change
Expand Up @@ -2681,8 +2681,15 @@ def sortlevel(
"""
Sort MultiIndex at the requested level.

The result will respect the original ordering of the associated
factor at that level.
This method is useful when dealing with MultiIndex objects, allowing for
sorting at a specific level of the index. The function preserves the
relative ordering of data within the same level while sorting
the overall MultiIndex. The method provides flexibility with the `ascending`
parameter to define the sort order and with the `sort_remaining` parameter to
control whether the remaining levels should also be sorted. Sorting a
MultiIndex can be crucial when performing operations that require ordered
indices, such as grouping or merging datasets. The `na_position` argument is
important in handling missing values consistently across different levels.

Parameters
----------
Expand All @@ -2692,7 +2699,9 @@ def sortlevel(
ascending : bool, default True
False to sort in descending order.
Can also be a list to specify a directed ordering.
sort_remaining : sort by the remaining levels after level
sort_remaining : bool, default True
If True, sorts by the remaining levels after sorting by the specified
`level`.
na_position : {'first' or 'last'}, default 'first'
Argument 'first' puts NaNs at the beginning, 'last' puts NaNs at
the end.
Expand All @@ -2706,6 +2715,13 @@ def sortlevel(
indexer : np.ndarray[np.intp]
Indices of output values in original index.

See Also
--------
MultiIndex : A multi-level, or hierarchical, index object for pandas objects.
Index.sort_values : Sort Index values.
DataFrame.sort_index : Sort DataFrame by the index.
Series.sort_index : Sort Series by the index.

Examples
--------
>>> mi = pd.MultiIndex.from_arrays([[0, 0], [2, 1]])
Expand Down