@@ -3863,23 +3863,79 @@ def nsmallest(self, n: int = 5, keep: str = "first") -> Series:
3863
3863
"""
3864
3864
return algorithms .SelectNSeries (self , n = n , keep = keep ).nsmallest ()
3865
3865
3866
+ @doc (
3867
+ klass = _shared_doc_kwargs ["klass" ],
3868
+ extra_params = dedent (
3869
+ """copy : bool, default True
3870
+ Whether to copy underlying data."""
3871
+ ),
3872
+ examples = dedent (
3873
+ """Examples
3874
+ --------
3875
+ >>> s = pd.Series(
3876
+ ... ["A", "B", "A", "C"],
3877
+ ... index=[
3878
+ ... ["Final exam", "Final exam", "Coursework", "Coursework"],
3879
+ ... ["History", "Geography", "History", "Geography"],
3880
+ ... ["January", "February", "March", "April"],
3881
+ ... ],
3882
+ ... )
3883
+ >>> s
3884
+ Final exam History January A
3885
+ Geography February B
3886
+ Coursework History March A
3887
+ Geography April C
3888
+
3889
+ In the following example, we will swap the levels of the indices.
3890
+ Here, we will swap the levels column-wise, but levels can be swapped row-wise
3891
+ in a similar manner. Note that column-wise is the default behaviour.
3892
+ By not supplying any arguments for i and j, we swap the last and second to
3893
+ last indices.
3894
+
3895
+ >>> s.swaplevel()
3896
+ Final exam January History A
3897
+ February Geography B
3898
+ Coursework March History A
3899
+ April Geography C
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
+
3911
+ We can also define explicitly which indices we want to swap by supplying values
3912
+ for both i and j. Here, we for example swap the first and second indices.
3913
+
3914
+ >>> s.swaplevel(0, 1)
3915
+ History Final exam January A
3916
+ Geography Final exam February B
3917
+ History Coursework March A
3918
+ Geography Coursework April C"""
3919
+ ),
3920
+ )
3866
3921
def swaplevel (self , i = - 2 , j = - 1 , copy = True ) -> Series :
3867
3922
"""
3868
- Swap levels i and j in a :class:`MultiIndex`.
3923
+ Swap levels i and j in a :class:`MultiIndex` on a particular axis .
3869
3924
3870
3925
Default is to swap the two innermost levels of the index.
3871
3926
3872
3927
Parameters
3873
3928
----------
3874
- i, j : int, str
3875
- Level of the indices to be swapped. Can pass level name as string.
3876
- copy : bool, default True
3877
- Whether to copy underlying data.
3929
+ i, j : int or str
3930
+ Levels of the indices to be swapped. Can pass level name as string.
3931
+ {extra_params}
3878
3932
3879
3933
Returns
3880
3934
-------
3881
- Series
3882
- Series with levels swapped in MultiIndex.
3935
+ {klass}
3936
+ {klass} with levels swapped in MultiIndex.
3937
+
3938
+ {examples}
3883
3939
"""
3884
3940
assert isinstance (self .index , MultiIndex )
3885
3941
new_index = self .index .swaplevel (i , j )
0 commit comments