Skip to content

Commit 76eb6b2

Browse files
DOC: Share swaplevel docstring between Series and DataFrame (pandas-dev#42120)
1 parent 98e2229 commit 76eb6b2

File tree

2 files changed

+76
-25
lines changed

2 files changed

+76
-25
lines changed

pandas/core/frame.py

+13-18
Original file line numberDiff line numberDiff line change
@@ -6740,23 +6740,16 @@ def nsmallest(self, n, columns, keep: str = "first") -> DataFrame:
67406740
self, n=n, keep=keep, columns=columns
67416741
).nsmallest()
67426742

6743-
def swaplevel(self, i: Axis = -2, j: Axis = -1, axis: Axis = 0) -> DataFrame:
6744-
"""
6745-
Swap levels i and j in a MultiIndex on a particular axis.
6746-
6747-
Parameters
6748-
----------
6749-
i, j : int or str
6750-
Levels of the indices to be swapped. Can pass level name as string.
6751-
axis : {0 or 'index', 1 or 'columns'}, default 0
6743+
@doc(
6744+
Series.swaplevel,
6745+
klass=_shared_doc_kwargs["klass"],
6746+
extra_params=dedent(
6747+
"""axis : {0 or 'index', 1 or 'columns'}, default 0
67526748
The axis to swap levels on. 0 or 'index' for row-wise, 1 or
6753-
'columns' for column-wise.
6754-
6755-
Returns
6756-
-------
6757-
DataFrame
6758-
6759-
Examples
6749+
'columns' for column-wise."""
6750+
),
6751+
examples=dedent(
6752+
"""Examples
67606753
--------
67616754
>>> df = pd.DataFrame(
67626755
... {"Grade": ["A", "B", "A", "C"]},
@@ -6805,8 +6798,10 @@ def swaplevel(self, i: Axis = -2, j: Axis = -1, axis: Axis = 0) -> DataFrame:
68056798
History Final exam January A
68066799
Geography Final exam February B
68076800
History Coursework March A
6808-
Geography Coursework April C
6809-
"""
6801+
Geography Coursework April C"""
6802+
),
6803+
)
6804+
def swaplevel(self, i: Axis = -2, j: Axis = -1, axis: Axis = 0) -> DataFrame:
68106805
result = self.copy()
68116806

68126807
axis = self._get_axis_number(axis)

pandas/core/series.py

+63-7
Original file line numberDiff line numberDiff line change
@@ -3863,23 +3863,79 @@ def nsmallest(self, n: int = 5, keep: str = "first") -> Series:
38633863
"""
38643864
return algorithms.SelectNSeries(self, n=n, keep=keep).nsmallest()
38653865

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+
)
38663921
def swaplevel(self, i=-2, j=-1, copy=True) -> Series:
38673922
"""
3868-
Swap levels i and j in a :class:`MultiIndex`.
3923+
Swap levels i and j in a :class:`MultiIndex` on a particular axis.
38693924
38703925
Default is to swap the two innermost levels of the index.
38713926
38723927
Parameters
38733928
----------
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}
38783932
38793933
Returns
38803934
-------
3881-
Series
3882-
Series with levels swapped in MultiIndex.
3935+
{klass}
3936+
{klass} with levels swapped in MultiIndex.
3937+
3938+
{examples}
38833939
"""
38843940
assert isinstance(self.index, MultiIndex)
38853941
new_index = self.index.swaplevel(i, j)

0 commit comments

Comments
 (0)