Skip to content

Commit fc32727

Browse files
dsakumajorisvandenbossche
authored andcommitted
DOC: update the pandas.Series.sort_index docstring (#20248)
1 parent 3bed3eb commit fc32727

File tree

1 file changed

+109
-1
lines changed

1 file changed

+109
-1
lines changed

pandas/core/series.py

+109-1
Original file line numberDiff line numberDiff line change
@@ -2230,10 +2230,118 @@ def _try_kind_sort(arr):
22302230
else:
22312231
return result.__finalize__(self)
22322232

2233-
@Appender(generic._shared_docs['sort_index'] % _shared_doc_kwargs)
22342233
def sort_index(self, axis=0, level=None, ascending=True, inplace=False,
22352234
kind='quicksort', na_position='last', sort_remaining=True):
2235+
"""
2236+
Sort Series by index labels.
2237+
2238+
Returns a new Series sorted by label if `inplace` argument is
2239+
``False``, otherwise updates the original series and returns None.
2240+
2241+
Parameters
2242+
----------
2243+
axis : int, default 0
2244+
Axis to direct sorting. This can only be 0 for Series.
2245+
level : int, optional
2246+
If not None, sort on values in specified index level(s).
2247+
ascending : bool, default true
2248+
Sort ascending vs. descending.
2249+
inplace : bool, default False
2250+
If True, perform operation in-place.
2251+
kind : {'quicksort', 'mergesort', 'heapsort'}, default 'quicksort'
2252+
Choice of sorting algorithm. See also :func:`numpy.sort` for more
2253+
information. 'mergesort' is the only stable algorithm. For
2254+
DataFrames, this option is only applied when sorting on a single
2255+
column or label.
2256+
na_position : {'first', 'last'}, default 'last'
2257+
If 'first' puts NaNs at the beginning, 'last' puts NaNs at the end.
2258+
Not implemented for MultiIndex.
2259+
sort_remaining : bool, default True
2260+
If true and sorting by level and index is multilevel, sort by other
2261+
levels too (in order) after sorting by specified level.
2262+
2263+
Returns
2264+
-------
2265+
pandas.Series
2266+
The original Series sorted by the labels
2267+
2268+
See Also
2269+
--------
2270+
DataFrame.sort_index: Sort DataFrame by the index
2271+
DataFrame.sort_values: Sort DataFrame by the value
2272+
Series.sort_values : Sort Series by the value
22362273
2274+
Examples
2275+
--------
2276+
>>> s = pd.Series(['a', 'b', 'c', 'd'], index=[3, 2, 1, 4])
2277+
>>> s.sort_index()
2278+
1 c
2279+
2 b
2280+
3 a
2281+
4 d
2282+
dtype: object
2283+
2284+
Sort Descending
2285+
2286+
>>> s.sort_index(ascending=False)
2287+
4 d
2288+
3 a
2289+
2 b
2290+
1 c
2291+
dtype: object
2292+
2293+
Sort Inplace
2294+
2295+
>>> s.sort_index(inplace=True)
2296+
>>> s
2297+
1 c
2298+
2 b
2299+
3 a
2300+
4 d
2301+
dtype: object
2302+
2303+
By default NaNs are put at the end, but use `na_position` to place
2304+
them at the beginning
2305+
2306+
>>> s = pd.Series(['a', 'b', 'c', 'd'], index=[3, 2, 1, np.nan])
2307+
>>> s.sort_index(na_position='first')
2308+
NaN d
2309+
1.0 c
2310+
2.0 b
2311+
3.0 a
2312+
dtype: object
2313+
2314+
Specify index level to sort
2315+
2316+
>>> arrays = [np.array(['qux', 'qux', 'foo', 'foo',
2317+
... 'baz', 'baz', 'bar', 'bar']),
2318+
... np.array(['two', 'one', 'two', 'one',
2319+
... 'two', 'one', 'two', 'one'])]
2320+
>>> s = pd.Series([1, 2, 3, 4, 5, 6, 7, 8], index=arrays)
2321+
>>> s.sort_index(level=1)
2322+
bar one 8
2323+
baz one 6
2324+
foo one 4
2325+
qux one 2
2326+
bar two 7
2327+
baz two 5
2328+
foo two 3
2329+
qux two 1
2330+
dtype: int64
2331+
2332+
Does not sort by remaining levels when sorting by levels
2333+
2334+
>>> s.sort_index(level=1, sort_remaining=False)
2335+
qux one 2
2336+
foo one 4
2337+
baz one 6
2338+
bar one 8
2339+
qux two 1
2340+
foo two 3
2341+
baz two 5
2342+
bar two 7
2343+
dtype: int64
2344+
"""
22372345
# TODO: this can be combined with DataFrame.sort_index impl as
22382346
# almost identical
22392347
inplace = validate_bool_kwarg(inplace, 'inplace')

0 commit comments

Comments
 (0)