@@ -3861,6 +3861,65 @@ def nsmallest(self, n: int = 5, keep: str = "first") -> Series:
38613861 """
38623862 return algorithms .SelectNSeries (self , n = n , keep = keep ).nsmallest ()
38633863
3864+ @doc (
3865+ klass = _shared_doc_kwargs ["klass" ],
3866+ extra_params = dedent (
3867+ """copy : bool, default True
3868+ Whether to copy underlying data."""
3869+ ),
3870+ examples = dedent (
3871+ """Examples
3872+ --------
3873+ >>> s = pd.Series(
3874+ ... ["A", "B", "A", "C"],
3875+ ... index=[
3876+ ... ["Final exam", "Final exam", "Coursework", "Coursework"],
3877+ ... ["History", "Geography", "History", "Geography"],
3878+ ... ["January", "February", "March", "April"],
3879+ ... ],
3880+ ... )
3881+ >>> s
3882+ Final exam History January A
3883+ Geography February B
3884+ Coursework History March A
3885+ Geography April C
3886+ dtype: object
3887+
3888+ In the following example, we will swap the levels of the indices.
3889+ Here, we will swap the levels column-wise, but levels can be swapped row-wise
3890+ in a similar manner. Note that column-wise is the default behaviour.
3891+ By not supplying any arguments for i and j, we swap the last and second to
3892+ last indices.
3893+
3894+ >>> s.swaplevel()
3895+ Final exam January History A
3896+ February Geography B
3897+ Coursework March History A
3898+ April Geography C
3899+ dtype: object
3900+
3901+ By supplying one argument, we can choose which index to swap the last
3902+ index with. We can for example swap the first index with the last one as
3903+ follows.
3904+
3905+ >>> s.swaplevel(0)
3906+ January History Final exam A
3907+ February Geography Final exam B
3908+ March History Coursework A
3909+ April Geography Coursework C
3910+ dtype: object
3911+
3912+ We can also define explicitly which indices we want to swap by supplying values
3913+ for both i and j. Here, we for example swap the first and second indices.
3914+
3915+ >>> s.swaplevel(0, 1)
3916+ History Final exam January A
3917+ Geography Final exam February B
3918+ History Coursework March A
3919+ Geography Coursework April C
3920+ dtype: object"""
3921+ ),
3922+ )
38643923 def swaplevel (self , i = - 2 , j = - 1 , copy = True ) -> Series :
38653924 """
38663925 Swap levels i and j in a :class:`MultiIndex`.
@@ -3869,15 +3928,16 @@ def swaplevel(self, i=-2, j=-1, copy=True) -> Series:
38693928
38703929 Parameters
38713930 ----------
3872- i, j : int, str
3873- Level of the indices to be swapped. Can pass level name as string.
3874- copy : bool, default True
3875- Whether to copy underlying data.
3931+ i, j : int or str
3932+ Levels of the indices to be swapped. Can pass level name as string.
3933+ {extra_params}
38763934
38773935 Returns
38783936 -------
3879- Series
3880- Series with levels swapped in MultiIndex.
3937+ {klass}
3938+ {klass} with levels swapped in MultiIndex.
3939+
3940+ {examples}
38813941 """
38823942 assert isinstance (self .index , MultiIndex )
38833943 new_index = self .index .swaplevel (i , j )
0 commit comments