Skip to content

DOC: fix SA01 error for pandas.Series.swaplevel #58736

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion ci/code_checks.sh
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,6 @@ if [[ -z "$CHECK" || "$CHECK" == "docstrings" ]]; then
-i "pandas.Series.str.zfill RT03" \
-i "pandas.Series.struct.dtypes SA01" \
-i "pandas.Series.sum RT03" \
-i "pandas.Series.swaplevel SA01" \
-i "pandas.Series.to_dict SA01" \
-i "pandas.Series.to_frame SA01" \
-i "pandas.Series.to_markdown SA01" \
Expand Down
78 changes: 37 additions & 41 deletions pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -3968,26 +3968,44 @@ def nsmallest(
"""
return selectn.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.
def swaplevel(
self, i: Level = -2, j: Level = -1, copy: bool | lib.NoDefault = lib.no_default
) -> Series:
"""
Swap levels i and j in a :class:`MultiIndex`.

.. note::
The `copy` keyword will change behavior in pandas 3.0.
`Copy-on-Write
<https://pandas.pydata.org/docs/dev/user_guide/copy_on_write.html>`__
will be enabled by default, which means that all methods with a
`copy` keyword will use a lazy copy mechanism to defer the copy and
ignore the `copy` keyword. The `copy` keyword will be removed in a
future version of pandas.
Default is to swap the two innermost levels of the index.

Parameters
----------
i, j : int or str
Levels of the indices to be swapped. Can pass level name as string.
copy : bool, default True
Whether to copy underlying data.

.. note::
The `copy` keyword will change behavior in pandas 3.0.
`Copy-on-Write
<https://pandas.pydata.org/docs/dev/user_guide/copy_on_write.html>`__
will be enabled by default, which means that all methods with a
`copy` keyword will use a lazy copy mechanism to defer the copy
and ignore the `copy` keyword. The `copy` keyword will be
removed in a future version of pandas.

You can already get the future behavior and improvements through
enabling copy on write ``pd.options.mode.copy_on_write = True``

Returns
-------
Series
Series with levels swapped in MultiIndex.

See Also
--------
DataFrame.swaplevel : Swap levels i and j in a :class:`DataFrame`.
Series.reorder_levels : Rearrange index levels using input order.
MultiIndex.swaplevel : Swap levels i and j in a :class:`MultiIndex`.

You can already get the future behavior and improvements through
enabling copy on write ``pd.options.mode.copy_on_write = True``"""
),
examples=dedent(
"""\
Examples
--------
>>> s = pd.Series(
Expand Down Expand Up @@ -4037,29 +4055,7 @@ def nsmallest(
Geography Final exam February B
History Coursework March A
Geography Coursework April C
dtype: object"""
),
)
def swaplevel(
self, i: Level = -2, j: Level = -1, copy: bool | lib.NoDefault = lib.no_default
) -> Series:
"""
Swap levels i and j in a :class:`MultiIndex`.

Default is to swap the two innermost levels of the index.

Parameters
----------
i, j : int or str
Levels of the indices to be swapped. Can pass level name as string.
{extra_params}

Returns
-------
{klass}
{klass} with levels swapped in MultiIndex.

{examples}
dtype: object
"""
self._check_copy_deprecation(copy)
assert isinstance(self.index, MultiIndex)
Expand Down