Skip to content

Commit 4706574

Browse files
DOC: updated inline documentation for key sorting
1 parent b0bc833 commit 4706574

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
@@ -5073,9 +5073,8 @@ def sort_index(
50735073
elif isinstance(labels, MultiIndex):
50745074
from pandas.core.sorting import lexsort_indexer
50755075

5076-
codes = labels._get_codes_for_sorting()
50775076
indexer = lexsort_indexer(
5078-
codes,
5077+
labels._get_codes_for_sorting(),
50795078
orders=ascending,
50805079
na_position=na_position,
50815080
)

pandas/core/series.py

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3042,6 +3042,9 @@ def sort_values(
30423042
na_position : {'first' or 'last'}, default 'last'
30433043
Argument 'first' puts NaNs at the beginning, 'last' puts NaNs at
30443044
the end.
3045+
key : function, default None
3046+
If not None, apply the key function to every value before
3047+
sorting. Identical to key argument in built-in sorted function.
30453048
30463049
Returns
30473050
-------
@@ -3124,6 +3127,22 @@ def sort_values(
31243127
2 d
31253128
0 z
31263129
dtype: object
3130+
3131+
>>> s = pd.Series(['a', 'B', 'c', 'D', 'e'])
3132+
>>> s.sort_values()
3133+
1 B
3134+
3 D
3135+
0 a
3136+
2 c
3137+
4 e
3138+
dtype: object
3139+
>>> s.sort_values(key=str.lower)
3140+
0 a
3141+
1 B
3142+
2 c
3143+
3 D
3144+
4 e
3145+
dtype: object
31273146
"""
31283147
inplace = validate_bool_kwarg(inplace, "inplace")
31293148
# Validate the axis parameter
@@ -3230,6 +3249,9 @@ def sort_index(
32303249
sort_remaining : bool, default True
32313250
If True and sorting by level and index is multilevel, sort by other
32323251
levels too (in order) after sorting by specified level.
3252+
key : function, default None
3253+
If not None, apply the key function to every index element before
3254+
sorting. Identical to key argument in built-in sorted function.
32333255
32343256
Returns
32353257
-------
@@ -3312,7 +3334,20 @@ def sort_index(
33123334
baz two 5
33133335
bar two 7
33143336
dtype: int64
3337+
3338+
>>> s = Series([1, 2, 3, 4, 5, 6, 7, 8])
3339+
>>> s.sort_index(key=lambda x : -x)
3340+
7 8
3341+
6 7
3342+
5 6
3343+
4 5
3344+
3 4
3345+
2 3
3346+
1 2
3347+
0 1
3348+
dtype: int64
33153349
"""
3350+
33163351
# TODO: this can be combined with DataFrame.sort_index impl as
33173352
# almost identical
33183353
inplace = validate_bool_kwarg(inplace, "inplace")
@@ -3332,10 +3367,9 @@ def sort_index(
33323367
from pandas.core.sorting import lexsort_indexer
33333368

33343369
labels = index._sort_levels_monotonic()
3335-
codes = labels._get_codes_for_sorting()
33363370

33373371
indexer = lexsort_indexer(
3338-
codes,
3372+
labels._get_codes_for_sorting(),
33393373
orders=ascending,
33403374
na_position=na_position,
33413375
)

0 commit comments

Comments
 (0)