Skip to content

Doc: Series.rename parameter documentation with Tests #48036

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 6 commits into from
Aug 12, 2022
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
20 changes: 13 additions & 7 deletions pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -4859,17 +4859,23 @@ def rename(

Parameters
----------
axis : {0 or 'index'}
Unused. Parameter needed for compatibility with DataFrame.
index : scalar, hashable sequence, dict-like or function, optional
index : scalar, hashable sequence, dict-like or function optional
Functions or dict-like are transformations to apply to
the index.
Scalar or hashable sequence-like will alter the ``Series.name``
attribute.

**kwargs
Additional keyword arguments passed to the function. Only the
"inplace" keyword is used.
axis : {0 or 'index'}
Unused. Parameter needed for compatibility with DataFrame.
copy : bool, default True
Also copy underlying data.
inplace : bool, default False
Whether to return a new Series. If True the value of copy is ignored.
level : int or level name, default None
In case of MultiIndex, only rename labels in the specified level.
errors : {'ignore', 'raise'}, default 'ignore'
If 'raise', raise `KeyError` when a `dict-like mapper` or
`index` contains labels that are not present in the index being transformed.
If 'ignore', existing keys will be renamed and extra keys will be ignored.

Returns
-------
Expand Down
16 changes: 16 additions & 0 deletions pandas/tests/series/methods/test_rename.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from datetime import datetime
import re

import numpy as np
import pytest
Expand Down Expand Up @@ -134,3 +135,18 @@ def test_rename_series_with_multiindex(self):
series_expected = Series(np.ones(5), index=index_expected)

tm.assert_series_equal(result, series_expected)

def test_rename_error_arg(self):
# GH 46889
ser = Series(["foo", "bar"])
match = re.escape("[2] not found in axis")
with pytest.raises(KeyError, match=match):
ser.rename({2: 9}, errors="raise")

def test_rename_copy_false(self):
# GH 46889
ser = Series(["foo", "bar"])
shallow_copy = ser.rename({1: 9}, copy=False)
ser[0] = "foobar"
assert ser[0] == shallow_copy[0]
assert ser[1] == shallow_copy[9]