Skip to content

Commit 5d2a135

Browse files
Backport PR pandas-dev#42148: DOC: share swaplevel docstring between DataFrame and Series (pandas-dev#42120) (pandas-dev#42257)
Co-authored-by: Gesa Stupperich <[email protected]>
1 parent c18f4f4 commit 5d2a135

File tree

2 files changed

+79
-24
lines changed

2 files changed

+79
-24
lines changed

pandas/core/frame.py

+13-18
Original file line numberDiff line numberDiff line change
@@ -6744,23 +6744,16 @@ def nsmallest(self, n, columns, keep: str = "first") -> DataFrame:
67446744
self, n=n, keep=keep, columns=columns
67456745
).nsmallest()
67466746

6747-
def swaplevel(self, i: Axis = -2, j: Axis = -1, axis: Axis = 0) -> DataFrame:
6748-
"""
6749-
Swap levels i and j in a MultiIndex on a particular axis.
6750-
6751-
Parameters
6752-
----------
6753-
i, j : int or str
6754-
Levels of the indices to be swapped. Can pass level name as string.
6755-
axis : {0 or 'index', 1 or 'columns'}, default 0
6747+
@doc(
6748+
Series.swaplevel,
6749+
klass=_shared_doc_kwargs["klass"],
6750+
extra_params=dedent(
6751+
"""axis : {0 or 'index', 1 or 'columns'}, default 0
67566752
The axis to swap levels on. 0 or 'index' for row-wise, 1 or
6757-
'columns' for column-wise.
6758-
6759-
Returns
6760-
-------
6761-
DataFrame
6762-
6763-
Examples
6753+
'columns' for column-wise."""
6754+
),
6755+
examples=dedent(
6756+
"""Examples
67646757
--------
67656758
>>> df = pd.DataFrame(
67666759
... {"Grade": ["A", "B", "A", "C"]},
@@ -6809,8 +6802,10 @@ def swaplevel(self, i: Axis = -2, j: Axis = -1, axis: Axis = 0) -> DataFrame:
68096802
History Final exam January A
68106803
Geography Final exam February B
68116804
History Coursework March A
6812-
Geography Coursework April C
6813-
"""
6805+
Geography Coursework April C"""
6806+
),
6807+
)
6808+
def swaplevel(self, i: Axis = -2, j: Axis = -1, axis: Axis = 0) -> DataFrame:
68146809
result = self.copy()
68156810

68166811
axis = self._get_axis_number(axis)

pandas/core/series.py

+66-6
Original file line numberDiff line numberDiff line change
@@ -3863,6 +3863,65 @@ 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+
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+
)
38663925
def swaplevel(self, i=-2, j=-1, copy=True) -> Series:
38673926
"""
38683927
Swap levels i and j in a :class:`MultiIndex`.
@@ -3871,15 +3930,16 @@ def swaplevel(self, i=-2, j=-1, copy=True) -> Series:
38713930
38723931
Parameters
38733932
----------
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}
38783936
38793937
Returns
38803938
-------
3881-
Series
3882-
Series with levels swapped in MultiIndex.
3939+
{klass}
3940+
{klass} with levels swapped in MultiIndex.
3941+
3942+
{examples}
38833943
"""
38843944
assert isinstance(self.index, MultiIndex)
38853945
new_index = self.index.swaplevel(i, j)

0 commit comments

Comments
 (0)