@@ -2861,6 +2861,9 @@ def sort_values(
2861
2861
na_position : {'first' or 'last'}, default 'last'
2862
2862
Argument 'first' puts NaNs at the beginning, 'last' puts NaNs at
2863
2863
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.
2864
2867
2865
2868
Returns
2866
2869
-------
@@ -2943,6 +2946,22 @@ def sort_values(
2943
2946
2 d
2944
2947
0 z
2945
2948
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
2946
2965
"""
2947
2966
inplace = validate_bool_kwarg (inplace , "inplace" )
2948
2967
# Validate the axis parameter
@@ -3048,6 +3067,9 @@ def sort_index(
3048
3067
sort_remaining : bool, default True
3049
3068
If True and sorting by level and index is multilevel, sort by other
3050
3069
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.
3051
3073
3052
3074
Returns
3053
3075
-------
@@ -3130,7 +3152,20 @@ def sort_index(
3130
3152
baz two 5
3131
3153
bar two 7
3132
3154
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
3133
3167
"""
3168
+
3134
3169
# TODO: this can be combined with DataFrame.sort_index impl as
3135
3170
# almost identical
3136
3171
inplace = validate_bool_kwarg (inplace , "inplace" )
@@ -3150,10 +3185,9 @@ def sort_index(
3150
3185
from pandas .core .sorting import lexsort_indexer
3151
3186
3152
3187
labels = index ._sort_levels_monotonic ()
3153
- codes = labels ._get_codes_for_sorting ()
3154
3188
3155
3189
indexer = lexsort_indexer (
3156
- codes ,
3190
+ labels . _get_codes_for_sorting () ,
3157
3191
orders = ascending ,
3158
3192
na_position = na_position ,
3159
3193
)
0 commit comments