Skip to content

Commit 3392360

Browse files
DataOmbudsmanTomAugspurger
authored andcommitted
DOC: update the Index.sort_values docstring (#20299)
* DOC: update the Index.sort_values docstring * Add better name to 2nd return value
1 parent c76120f commit 3392360

File tree

1 file changed

+40
-1
lines changed

1 file changed

+40
-1
lines changed

pandas/core/indexes/base.py

+40-1
Original file line numberDiff line numberDiff line change
@@ -2344,7 +2344,46 @@ def asof_locs(self, where, mask):
23442344

23452345
def sort_values(self, return_indexer=False, ascending=True):
23462346
"""
2347-
Return sorted copy of Index
2347+
Return a sorted copy of the index.
2348+
2349+
Return a sorted copy of the index, and optionally return the indices
2350+
that sorted the index itself.
2351+
2352+
Parameters
2353+
----------
2354+
return_indexer : bool, default False
2355+
Should the indices that would sort the index be returned.
2356+
ascending : bool, default True
2357+
Should the index values be sorted in an ascending order.
2358+
2359+
Returns
2360+
-------
2361+
sorted_index : pandas.Index
2362+
Sorted copy of the index.
2363+
indexer : numpy.ndarray, optional
2364+
The indices that the index itself was sorted by.
2365+
2366+
See Also
2367+
--------
2368+
pandas.Series.sort_values : Sort values of a Series.
2369+
pandas.DataFrame.sort_values : Sort values in a DataFrame.
2370+
2371+
Examples
2372+
--------
2373+
>>> idx = pd.Index([10, 100, 1, 1000])
2374+
>>> idx
2375+
Int64Index([10, 100, 1, 1000], dtype='int64')
2376+
2377+
Sort values in ascending order (default behavior).
2378+
2379+
>>> idx.sort_values()
2380+
Int64Index([1, 10, 100, 1000], dtype='int64')
2381+
2382+
Sort values in descending order, and also get the indices `idx` was
2383+
sorted by.
2384+
2385+
>>> idx.sort_values(ascending=False, return_indexer=True)
2386+
(Int64Index([1000, 100, 10, 1], dtype='int64'), array([3, 1, 0, 2]))
23482387
"""
23492388
_as = self.argsort()
23502389
if not ascending:

0 commit comments

Comments
 (0)