@@ -3042,6 +3042,9 @@ def sort_values(
3042
3042
na_position : {'first' or 'last'}, default 'last'
3043
3043
Argument 'first' puts NaNs at the beginning, 'last' puts NaNs at
3044
3044
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.
3045
3048
3046
3049
Returns
3047
3050
-------
@@ -3124,6 +3127,22 @@ def sort_values(
3124
3127
2 d
3125
3128
0 z
3126
3129
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
3127
3146
"""
3128
3147
inplace = validate_bool_kwarg (inplace , "inplace" )
3129
3148
# Validate the axis parameter
@@ -3230,6 +3249,9 @@ def sort_index(
3230
3249
sort_remaining : bool, default True
3231
3250
If True and sorting by level and index is multilevel, sort by other
3232
3251
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.
3233
3255
3234
3256
Returns
3235
3257
-------
@@ -3312,7 +3334,20 @@ def sort_index(
3312
3334
baz two 5
3313
3335
bar two 7
3314
3336
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
3315
3349
"""
3350
+
3316
3351
# TODO: this can be combined with DataFrame.sort_index impl as
3317
3352
# almost identical
3318
3353
inplace = validate_bool_kwarg (inplace , "inplace" )
@@ -3332,10 +3367,9 @@ def sort_index(
3332
3367
from pandas .core .sorting import lexsort_indexer
3333
3368
3334
3369
labels = index ._sort_levels_monotonic ()
3335
- codes = labels ._get_codes_for_sorting ()
3336
3370
3337
3371
indexer = lexsort_indexer (
3338
- codes ,
3372
+ labels . _get_codes_for_sorting () ,
3339
3373
orders = ascending ,
3340
3374
na_position = na_position ,
3341
3375
)
0 commit comments