Skip to content

Commit 9ca1058

Browse files
GH61998 Add documentation to pd.MultiIndex.argsort
1 parent d4ae649 commit 9ca1058

File tree

1 file changed

+47
-3
lines changed

1 file changed

+47
-3
lines changed

pandas/core/indexes/multi.py

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
IgnoreRaise,
4040
IndexLabel,
4141
IndexT,
42+
NaPosition,
4243
Scalar,
4344
Self,
4445
Shape,
@@ -2398,8 +2399,52 @@ 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+
Index(['a', 'b', 'c', 'd'], dtype='object')
2440+
2441+
>>> midx = pd.MultiIndex.from_arrays([[2, 2], [np.nan, 0]])
2442+
>>> midx.argsort(na_position='first')
2443+
array([0, 1])
2444+
2445+
>>> midx.argsort()
2446+
array([1, 0])
2447+
"""
24032448
target = self._sort_levels_monotonic(raise_if_incomparable=True)
24042449
keys = [lev.codes for lev in target._get_codes_for_sorting()]
24052450
return lexsort_indexer(keys, na_position=na_position, codes_given=True)
@@ -2791,8 +2836,7 @@ def sortlevel(
27912836
# error: Item "Hashable" of "Union[Hashable, Sequence[Hashable]]" has
27922837
# no attribute "__iter__" (not iterable)
27932838
level = [
2794-
self._get_level_number(lev)
2795-
for lev in level # type: ignore[union-attr]
2839+
self._get_level_number(lev) for lev in level # type: ignore[union-attr]
27962840
]
27972841
sortorder = None
27982842

0 commit comments

Comments
 (0)