@@ -2913,6 +2913,9 @@ def sort_values(
2913
2913
na_position : {'first' or 'last'}, default 'last'
2914
2914
Argument 'first' puts NaNs at the beginning, 'last' puts NaNs at
2915
2915
the end.
2916
+ key : function, default None
2917
+ If not None, apply the key function to every value before
2918
+ sorting. Identical to key argument in built-in sorted function.
2916
2919
2917
2920
Returns
2918
2921
-------
@@ -2995,6 +2998,22 @@ def sort_values(
2995
2998
2 d
2996
2999
0 z
2997
3000
dtype: object
3001
+
3002
+ >>> s = pd.Series(['a', 'B', 'c', 'D', 'e'])
3003
+ >>> s.sort_values()
3004
+ 1 B
3005
+ 3 D
3006
+ 0 a
3007
+ 2 c
3008
+ 4 e
3009
+ dtype: object
3010
+ >>> s.sort_values(key=str.lower)
3011
+ 0 a
3012
+ 1 B
3013
+ 2 c
3014
+ 3 D
3015
+ 4 e
3016
+ dtype: object
2998
3017
"""
2999
3018
inplace = validate_bool_kwarg (inplace , "inplace" )
3000
3019
# Validate the axis parameter
@@ -3100,6 +3119,9 @@ def sort_index(
3100
3119
sort_remaining : bool, default True
3101
3120
If True and sorting by level and index is multilevel, sort by other
3102
3121
levels too (in order) after sorting by specified level.
3122
+ key : function, default None
3123
+ If not None, apply the key function to every index element before
3124
+ sorting. Identical to key argument in built-in sorted function.
3103
3125
3104
3126
Returns
3105
3127
-------
@@ -3182,7 +3204,20 @@ def sort_index(
3182
3204
baz two 5
3183
3205
bar two 7
3184
3206
dtype: int64
3207
+
3208
+ >>> s = Series([1, 2, 3, 4, 5, 6, 7, 8])
3209
+ >>> s.sort_index(key=lambda x : -x)
3210
+ 7 8
3211
+ 6 7
3212
+ 5 6
3213
+ 4 5
3214
+ 3 4
3215
+ 2 3
3216
+ 1 2
3217
+ 0 1
3218
+ dtype: int64
3185
3219
"""
3220
+
3186
3221
# TODO: this can be combined with DataFrame.sort_index impl as
3187
3222
# almost identical
3188
3223
inplace = validate_bool_kwarg (inplace , "inplace" )
@@ -3202,10 +3237,9 @@ def sort_index(
3202
3237
from pandas .core .sorting import lexsort_indexer
3203
3238
3204
3239
labels = index ._sort_levels_monotonic ()
3205
- codes = labels ._get_codes_for_sorting ()
3206
3240
3207
3241
indexer = lexsort_indexer (
3208
- codes ,
3242
+ labels . _get_codes_for_sorting () ,
3209
3243
orders = ascending ,
3210
3244
na_position = na_position ,
3211
3245
)
0 commit comments