@@ -2708,14 +2708,18 @@ def swaplevel(self, i=-2, j=-1) -> MultiIndex:
27082708
27092709 Calling this method does not change the ordering of the values.
27102710
2711+ Default is to swap the last two levels of the MultiIndex.
2712+
27112713 Parameters
27122714 ----------
27132715 i : int, str, default -2
27142716 First level of index to be swapped. Can pass level name as string.
2715- Type of parameters can be mixed.
2717+ Type of parameters can be mixed. If i is a negative int, the first
2718+ level is indexed relative to the end of the MultiIndex.
27162719 j : int, str, default -1
27172720 Second level of index to be swapped. Can pass level name as string.
2718- Type of parameters can be mixed.
2721+ Type of parameters can be mixed. If j is a negative int, the second
2722+ level is indexed relative to the end of the MultiIndex.
27192723
27202724 Returns
27212725 -------
@@ -2731,20 +2735,33 @@ def swaplevel(self, i=-2, j=-1) -> MultiIndex:
27312735 Examples
27322736 --------
27332737 >>> mi = pd.MultiIndex(
2734- ... levels=[["a", "b"], ["bb", "aa"]], codes=[[0, 0, 1, 1], [0, 1, 0, 1]]
2738+ ... levels=[["a", "b"], ["bb", "aa"], ["aaa", "bbb"]],
2739+ ... codes=[[0, 0, 1, 1], [0, 1, 0, 1], [1, 0, 1, 0]],
27352740 ... )
27362741 >>> mi
2737- MultiIndex([('a', 'bb'),
2738- ('a', 'aa'),
2739- ('b', 'bb'),
2740- ('b', 'aa')],
2742+ MultiIndex([('a', 'bb', 'bbb' ),
2743+ ('a', 'aa', 'aaa' ),
2744+ ('b', 'bb', 'bbb' ),
2745+ ('b', 'aa', 'aaa' )],
27412746 )
2742- >>> mi.swaplevel(0, 1)
2743- MultiIndex([('bb', 'a'),
2744- ('aa', 'a'),
2745- ('bb', 'b'),
2746- ('aa', 'b')],
2747+ >>> mi.swaplevel()
2748+ MultiIndex([('a', 'bbb', 'bb'),
2749+ ('a', 'aaa', 'aa'),
2750+ ('b', 'bbb', 'bb'),
2751+ ('b', 'aaa', 'aa')],
2752+ )
2753+ >>> mi.swaplevel(0)
2754+ MultiIndex([('bbb', 'bb', 'a'),
2755+ ('aaa', 'aa', 'a'),
2756+ ('bbb', 'bb', 'b'),
2757+ ('aaa', 'aa', 'b')],
27472758 )
2759+ >>> mi.swaplevel(0, 1)
2760+ MultiIndex([('bb', 'a', 'bbb'),
2761+ ('aa', 'a', 'aaa'),
2762+ ('bb', 'b', 'bbb'),
2763+ ('aa', 'b', 'aaa')],
2764+ )
27482765 """
27492766 new_levels = list (self .levels )
27502767 new_codes = list (self .codes )
0 commit comments