Skip to content

Commit c190723

Browse files
authored
Merge pull request #210 from pandas-dev/master
DOC: share swaplevel docstring between DataFrame and Series (pandas-dev#42120) …
2 parents 6f5e923 + fa6b96e commit c190723

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
@@ -6738,23 +6738,16 @@ def nsmallest(self, n, columns, keep: str = "first") -> DataFrame:
67386738
self, n=n, keep=keep, columns=columns
67396739
).nsmallest()
67406740

6741-
def swaplevel(self, i: Axis = -2, j: Axis = -1, axis: Axis = 0) -> DataFrame:
6742-
"""
6743-
Swap levels i and j in a MultiIndex on a particular axis.
6744-
6745-
Parameters
6746-
----------
6747-
i, j : int or str
6748-
Levels of the indices to be swapped. Can pass level name as string.
6749-
axis : {0 or 'index', 1 or 'columns'}, default 0
6741+
@doc(
6742+
Series.swaplevel,
6743+
klass=_shared_doc_kwargs["klass"],
6744+
extra_params=dedent(
6745+
"""axis : {0 or 'index', 1 or 'columns'}, default 0
67506746
The axis to swap levels on. 0 or 'index' for row-wise, 1 or
6751-
'columns' for column-wise.
6752-
6753-
Returns
6754-
-------
6755-
DataFrame
6756-
6757-
Examples
6747+
'columns' for column-wise."""
6748+
),
6749+
examples=dedent(
6750+
"""Examples
67586751
--------
67596752
>>> df = pd.DataFrame(
67606753
... {"Grade": ["A", "B", "A", "C"]},
@@ -6803,8 +6796,10 @@ def swaplevel(self, i: Axis = -2, j: Axis = -1, axis: Axis = 0) -> DataFrame:
68036796
History Final exam January A
68046797
Geography Final exam February B
68056798
History Coursework March A
6806-
Geography Coursework April C
6807-
"""
6799+
Geography Coursework April C"""
6800+
),
6801+
)
6802+
def swaplevel(self, i: Axis = -2, j: Axis = -1, axis: Axis = 0) -> DataFrame:
68086803
result = self.copy()
68096804

68106805
axis = self._get_axis_number(axis)

pandas/core/series.py

+66-6
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)