Skip to content

Commit 6d0899c

Browse files
DOC: updated inline documentation for key sorting
1 parent ba2e043 commit 6d0899c

File tree

2 files changed

+37
-4
lines changed

2 files changed

+37
-4
lines changed

pandas/core/frame.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4817,9 +4817,8 @@ def sort_index(
48174817
elif isinstance(labels, ABCMultiIndex):
48184818
from pandas.core.sorting import lexsort_indexer
48194819

4820-
codes = labels._get_codes_for_sorting()
48214820
indexer = lexsort_indexer(
4822-
codes,
4821+
labels._get_codes_for_sorting(),
48234822
orders=ascending,
48244823
na_position=na_position,
48254824
)

pandas/core/series.py

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2861,6 +2861,9 @@ def sort_values(
28612861
na_position : {'first' or 'last'}, default 'last'
28622862
Argument 'first' puts NaNs at the beginning, 'last' puts NaNs at
28632863
the end.
2864+
key : function, default None
2865+
If not None, apply the key function to every value before
2866+
sorting. Identical to key argument in built-in sorted function.
28642867
28652868
Returns
28662869
-------
@@ -2943,6 +2946,22 @@ def sort_values(
29432946
2 d
29442947
0 z
29452948
dtype: object
2949+
2950+
>>> s = pd.Series(['a', 'B', 'c', 'D', 'e'])
2951+
>>> s.sort_values()
2952+
1 B
2953+
3 D
2954+
0 a
2955+
2 c
2956+
4 e
2957+
dtype: object
2958+
>>> s.sort_values(key=str.lower)
2959+
0 a
2960+
1 B
2961+
2 c
2962+
3 D
2963+
4 e
2964+
dtype: object
29462965
"""
29472966
inplace = validate_bool_kwarg(inplace, "inplace")
29482967
# Validate the axis parameter
@@ -3048,6 +3067,9 @@ def sort_index(
30483067
sort_remaining : bool, default True
30493068
If True and sorting by level and index is multilevel, sort by other
30503069
levels too (in order) after sorting by specified level.
3070+
key : function, default None
3071+
If not None, apply the key function to every index element before
3072+
sorting. Identical to key argument in built-in sorted function.
30513073
30523074
Returns
30533075
-------
@@ -3130,7 +3152,20 @@ def sort_index(
31303152
baz two 5
31313153
bar two 7
31323154
dtype: int64
3155+
3156+
>>> s = Series([1, 2, 3, 4, 5, 6, 7, 8])
3157+
>>> s.sort_index(key=lambda x : -x)
3158+
7 8
3159+
6 7
3160+
5 6
3161+
4 5
3162+
3 4
3163+
2 3
3164+
1 2
3165+
0 1
3166+
dtype: int64
31333167
"""
3168+
31343169
# TODO: this can be combined with DataFrame.sort_index impl as
31353170
# almost identical
31363171
inplace = validate_bool_kwarg(inplace, "inplace")
@@ -3150,10 +3185,9 @@ def sort_index(
31503185
from pandas.core.sorting import lexsort_indexer
31513186

31523187
labels = index._sort_levels_monotonic()
3153-
codes = labels._get_codes_for_sorting()
31543188

31553189
indexer = lexsort_indexer(
3156-
codes,
3190+
labels._get_codes_for_sorting(),
31573191
orders=ascending,
31583192
na_position=na_position,
31593193
)

0 commit comments

Comments
 (0)