Skip to content

Commit 986a8fb

Browse files
DOC: documenting pandas.MultIndex.argsort (#62005)
1 parent 006e6a6 commit 986a8fb

File tree

1 file changed

+48
-1
lines changed

1 file changed

+48
-1
lines changed

pandas/core/indexes/multi.py

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
IgnoreRaise,
4141
IndexLabel,
4242
IndexT,
43+
NaPosition,
4344
Scalar,
4445
Shape,
4546
npt,
@@ -2398,8 +2399,54 @@ def append(self, other):
23982399
return Index(new_tuples)
23992400

24002401
def argsort(
2401-
self, *args, na_position: str = "last", **kwargs
2402+
self, *args, na_position: NaPosition = "last", **kwargs
24022403
) -> npt.NDArray[np.intp]:
2404+
"""
2405+
Return the integer indices that would sort the index.
2406+
2407+
Parameters
2408+
----------
2409+
*args
2410+
Passed to `numpy.ndarray.argsort`.
2411+
na_position : {'first' or 'last'}, default 'last'
2412+
Argument 'first' puts NaNs at the beginning, 'last' puts NaNs at
2413+
the end.
2414+
**kwargs
2415+
Passed to `numpy.ndarray.argsort`.
2416+
2417+
Returns
2418+
-------
2419+
np.ndarray[np.intp]
2420+
Integer indices that would sort the index if used as
2421+
an indexer.
2422+
2423+
See Also
2424+
--------
2425+
numpy.argsort : Similar method for NumPy arrays.
2426+
Index.argsort : Similar method for Index.
2427+
2428+
Examples
2429+
--------
2430+
>>> midx = pd.MultiIndex.from_arrays([[3, 2], ["e", "c"]])
2431+
>>> midx
2432+
MultiIndex([(3, 'e'), (2, 'c')])
2433+
2434+
>>> order = midx.argsort()
2435+
>>> order
2436+
array([1, 0])
2437+
2438+
>>> midx[order]
2439+
MultiIndex([(2, 'c'),
2440+
(3, 'e')],
2441+
)
2442+
2443+
>>> midx = pd.MultiIndex.from_arrays([[2, 2], [np.nan, 0]])
2444+
>>> midx.argsort(na_position="first")
2445+
array([0, 1])
2446+
2447+
>>> midx.argsort()
2448+
array([1, 0])
2449+
"""
24032450
target = self._sort_levels_monotonic(raise_if_incomparable=True)
24042451
keys = [lev.codes for lev in target._get_codes_for_sorting()]
24052452
return lexsort_indexer(keys, na_position=na_position, codes_given=True)

0 commit comments

Comments
 (0)