From 01b6609e61e1667268fc90d891ae0a7d17245ca0 Mon Sep 17 00:00:00 2001 From: Gesa Stupperich Date: Sat, 26 Jun 2021 19:32:04 +0100 Subject: [PATCH] Backport PR #42148: DOC: share swaplevel docstring between DataFrame and Series (#42120) --- pandas/core/frame.py | 31 ++++++++----------- pandas/core/series.py | 72 +++++++++++++++++++++++++++++++++++++++---- 2 files changed, 79 insertions(+), 24 deletions(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 0dc844fe3d58d..e555625dbba95 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -6744,23 +6744,16 @@ def nsmallest(self, n, columns, keep: str = "first") -> DataFrame: self, n=n, keep=keep, columns=columns ).nsmallest() - def swaplevel(self, i: Axis = -2, j: Axis = -1, axis: Axis = 0) -> DataFrame: - """ - Swap levels i and j in a MultiIndex on a particular axis. - - Parameters - ---------- - i, j : int or str - Levels of the indices to be swapped. Can pass level name as string. - axis : {0 or 'index', 1 or 'columns'}, default 0 + @doc( + Series.swaplevel, + klass=_shared_doc_kwargs["klass"], + extra_params=dedent( + """axis : {0 or 'index', 1 or 'columns'}, default 0 The axis to swap levels on. 0 or 'index' for row-wise, 1 or - 'columns' for column-wise. - - Returns - ------- - DataFrame - - Examples + 'columns' for column-wise.""" + ), + examples=dedent( + """Examples -------- >>> df = pd.DataFrame( ... {"Grade": ["A", "B", "A", "C"]}, @@ -6809,8 +6802,10 @@ def swaplevel(self, i: Axis = -2, j: Axis = -1, axis: Axis = 0) -> DataFrame: History Final exam January A Geography Final exam February B History Coursework March A - Geography Coursework April C - """ + Geography Coursework April C""" + ), + ) + def swaplevel(self, i: Axis = -2, j: Axis = -1, axis: Axis = 0) -> DataFrame: result = self.copy() axis = self._get_axis_number(axis) diff --git a/pandas/core/series.py b/pandas/core/series.py index 800d2d736013a..9b21f4fa967ec 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -3863,6 +3863,65 @@ def nsmallest(self, n: int = 5, keep: str = "first") -> Series: """ return algorithms.SelectNSeries(self, n=n, keep=keep).nsmallest() + @doc( + klass=_shared_doc_kwargs["klass"], + extra_params=dedent( + """copy : bool, default True + Whether to copy underlying data.""" + ), + examples=dedent( + """Examples + -------- + >>> s = pd.Series( + ... ["A", "B", "A", "C"], + ... index=[ + ... ["Final exam", "Final exam", "Coursework", "Coursework"], + ... ["History", "Geography", "History", "Geography"], + ... ["January", "February", "March", "April"], + ... ], + ... ) + >>> s + Final exam History January A + Geography February B + Coursework History March A + Geography April C + dtype: object + + In the following example, we will swap the levels of the indices. + Here, we will swap the levels column-wise, but levels can be swapped row-wise + in a similar manner. Note that column-wise is the default behaviour. + By not supplying any arguments for i and j, we swap the last and second to + last indices. + + >>> s.swaplevel() + Final exam January History A + February Geography B + Coursework March History A + April Geography C + dtype: object + + By supplying one argument, we can choose which index to swap the last + index with. We can for example swap the first index with the last one as + follows. + + >>> s.swaplevel(0) + January History Final exam A + February Geography Final exam B + March History Coursework A + April Geography Coursework C + dtype: object + + We can also define explicitly which indices we want to swap by supplying values + for both i and j. Here, we for example swap the first and second indices. + + >>> s.swaplevel(0, 1) + History Final exam January A + Geography Final exam February B + History Coursework March A + Geography Coursework April C + dtype: object""" + ), + ) def swaplevel(self, i=-2, j=-1, copy=True) -> Series: """ Swap levels i and j in a :class:`MultiIndex`. @@ -3871,15 +3930,16 @@ def swaplevel(self, i=-2, j=-1, copy=True) -> Series: Parameters ---------- - i, j : int, str - Level of the indices to be swapped. Can pass level name as string. - copy : bool, default True - Whether to copy underlying data. + i, j : int or str + Levels of the indices to be swapped. Can pass level name as string. + {extra_params} Returns ------- - Series - Series with levels swapped in MultiIndex. + {klass} + {klass} with levels swapped in MultiIndex. + + {examples} """ assert isinstance(self.index, MultiIndex) new_index = self.index.swaplevel(i, j)