@@ -2835,7 +2835,7 @@ def sort_values(
2835
2835
inplace = False ,
2836
2836
kind = "quicksort" ,
2837
2837
na_position = "last" ,
2838
- key = None
2838
+ key = None ,
2839
2839
):
2840
2840
"""
2841
2841
Sort by the values.
@@ -2858,6 +2858,9 @@ def sort_values(
2858
2858
na_position : {'first' or 'last'}, default 'last'
2859
2859
Argument 'first' puts NaNs at the beginning, 'last' puts NaNs at
2860
2860
the end.
2861
+ key : Callable, default None
2862
+ If not None, apply the key function to every value before
2863
+ sorting. Identical to key argument in built-in sorted function.
2861
2864
2862
2865
Returns
2863
2866
-------
@@ -2940,6 +2943,22 @@ def sort_values(
2940
2943
2 d
2941
2944
0 z
2942
2945
dtype: object
2946
+
2947
+ >>> s = pd.Series(['a', 'B', 'c', 'D', 'e'])
2948
+ >>> s.sort_values()
2949
+ 1 B
2950
+ 3 D
2951
+ 0 a
2952
+ 2 c
2953
+ 4 e
2954
+ dtype: object
2955
+ >>> s.sort_values(key=str.lower)
2956
+ 0 a
2957
+ 1 B
2958
+ 2 c
2959
+ 3 D
2960
+ 4 e
2961
+ dtype: object
2943
2962
"""
2944
2963
inplace = validate_bool_kwarg (inplace , "inplace" )
2945
2964
# Validate the axis parameter
@@ -3016,7 +3035,7 @@ def sort_index(
3016
3035
kind = "quicksort" ,
3017
3036
na_position = "last" ,
3018
3037
sort_remaining = True ,
3019
- key = None
3038
+ key : Optional [ Callable ] = None ,
3020
3039
):
3021
3040
"""
3022
3041
Sort Series by index labels.
@@ -3045,6 +3064,9 @@ def sort_index(
3045
3064
sort_remaining : bool, default True
3046
3065
If True and sorting by level and index is multilevel, sort by other
3047
3066
levels too (in order) after sorting by specified level.
3067
+ key : Callable, default None
3068
+ If not None, apply the key function to every index element before
3069
+ sorting. Identical to key argument in built-in sorted function.
3048
3070
3049
3071
Returns
3050
3072
-------
@@ -3127,7 +3149,20 @@ def sort_index(
3127
3149
baz two 5
3128
3150
bar two 7
3129
3151
dtype: int64
3152
+
3153
+ >>> s = Series([1, 2, 3, 4, 5, 6, 7, 8])
3154
+ >>> s.sort_index(key=lambda x : -x)
3155
+ 7 8
3156
+ 6 7
3157
+ 5 6
3158
+ 4 5
3159
+ 3 4
3160
+ 2 3
3161
+ 1 2
3162
+ 0 1
3163
+ dtype: int64
3130
3164
"""
3165
+
3131
3166
# TODO: this can be combined with DataFrame.sort_index impl as
3132
3167
# almost identical
3133
3168
inplace = validate_bool_kwarg (inplace , "inplace" )
@@ -3136,8 +3171,8 @@ def sort_index(
3136
3171
index = self .index
3137
3172
true_index = index
3138
3173
if key is not None :
3139
- index = index .map (key )
3140
-
3174
+ index = index .map (key , na_action = "ignore" )
3175
+
3141
3176
if level is not None :
3142
3177
new_index , indexer = index .sortlevel (
3143
3178
level , ascending = ascending , sort_remaining = sort_remaining
@@ -3147,10 +3182,9 @@ def sort_index(
3147
3182
from pandas .core .sorting import lexsort_indexer
3148
3183
3149
3184
labels = index ._sort_levels_monotonic ()
3150
- codes = labels ._get_codes_for_sorting ()
3151
3185
3152
3186
indexer = lexsort_indexer (
3153
- codes ,
3187
+ labels . _get_codes_for_sorting () ,
3154
3188
orders = ascending ,
3155
3189
na_position = na_position ,
3156
3190
)
0 commit comments