@@ -3863,6 +3863,65 @@ 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
+ dtype: object
3889
+
3890
+ In the following example, we will swap the levels of the indices.
3891
+ Here, we will swap the levels column-wise, but levels can be swapped row-wise
3892
+ in a similar manner. Note that column-wise is the default behaviour.
3893
+ By not supplying any arguments for i and j, we swap the last and second to
3894
+ last indices.
3895
+
3896
+ >>> s.swaplevel()
3897
+ Final exam January History A
3898
+ February Geography B
3899
+ Coursework March History A
3900
+ April Geography C
3901
+ dtype: object
3902
+
3903
+ By supplying one argument, we can choose which index to swap the last
3904
+ index with. We can for example swap the first index with the last one as
3905
+ follows.
3906
+
3907
+ >>> s.swaplevel(0)
3908
+ January History Final exam A
3909
+ February Geography Final exam B
3910
+ March History Coursework A
3911
+ April Geography Coursework C
3912
+ dtype: object
3913
+
3914
+ We can also define explicitly which indices we want to swap by supplying values
3915
+ for both i and j. Here, we for example swap the first and second indices.
3916
+
3917
+ >>> s.swaplevel(0, 1)
3918
+ History Final exam January A
3919
+ Geography Final exam February B
3920
+ History Coursework March A
3921
+ Geography Coursework April C
3922
+ dtype: object"""
3923
+ ),
3924
+ )
3866
3925
def swaplevel (self , i = - 2 , j = - 1 , copy = True ) -> Series :
3867
3926
"""
3868
3927
Swap levels i and j in a :class:`MultiIndex`.
@@ -3871,15 +3930,16 @@ def swaplevel(self, i=-2, j=-1, copy=True) -> Series:
3871
3930
3872
3931
Parameters
3873
3932
----------
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.
3933
+ i, j : int or str
3934
+ Levels of the indices to be swapped. Can pass level name as string.
3935
+ {extra_params}
3878
3936
3879
3937
Returns
3880
3938
-------
3881
- Series
3882
- Series with levels swapped in MultiIndex.
3939
+ {klass}
3940
+ {klass} with levels swapped in MultiIndex.
3941
+
3942
+ {examples}
3883
3943
"""
3884
3944
assert isinstance (self .index , MultiIndex )
3885
3945
new_index = self .index .swaplevel (i , j )
0 commit comments