Skip to content

Commit ddbb6c2

Browse files
authored
Doc: Series.rename parameter documentation with Tests (#48036)
* add documentation to series rename func for args available to it * add tests for rename error and copy arguments * fix PEP 8 issues 'line too long' * fix whitespace * fix doc string parameter order * fix doc string
1 parent 289224d commit ddbb6c2

File tree

2 files changed

+29
-7
lines changed

2 files changed

+29
-7
lines changed

pandas/core/series.py

+13-7
Original file line numberDiff line numberDiff line change
@@ -4862,17 +4862,23 @@ def rename(
48624862
48634863
Parameters
48644864
----------
4865-
axis : {0 or 'index'}
4866-
Unused. Parameter needed for compatibility with DataFrame.
4867-
index : scalar, hashable sequence, dict-like or function, optional
4865+
index : scalar, hashable sequence, dict-like or function optional
48684866
Functions or dict-like are transformations to apply to
48694867
the index.
48704868
Scalar or hashable sequence-like will alter the ``Series.name``
48714869
attribute.
4872-
4873-
**kwargs
4874-
Additional keyword arguments passed to the function. Only the
4875-
"inplace" keyword is used.
4870+
axis : {0 or 'index'}
4871+
Unused. Parameter needed for compatibility with DataFrame.
4872+
copy : bool, default True
4873+
Also copy underlying data.
4874+
inplace : bool, default False
4875+
Whether to return a new Series. If True the value of copy is ignored.
4876+
level : int or level name, default None
4877+
In case of MultiIndex, only rename labels in the specified level.
4878+
errors : {'ignore', 'raise'}, default 'ignore'
4879+
If 'raise', raise `KeyError` when a `dict-like mapper` or
4880+
`index` contains labels that are not present in the index being transformed.
4881+
If 'ignore', existing keys will be renamed and extra keys will be ignored.
48764882
48774883
Returns
48784884
-------

pandas/tests/series/methods/test_rename.py

+16
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from datetime import datetime
2+
import re
23

34
import numpy as np
45
import pytest
@@ -134,3 +135,18 @@ def test_rename_series_with_multiindex(self):
134135
series_expected = Series(np.ones(5), index=index_expected)
135136

136137
tm.assert_series_equal(result, series_expected)
138+
139+
def test_rename_error_arg(self):
140+
# GH 46889
141+
ser = Series(["foo", "bar"])
142+
match = re.escape("[2] not found in axis")
143+
with pytest.raises(KeyError, match=match):
144+
ser.rename({2: 9}, errors="raise")
145+
146+
def test_rename_copy_false(self):
147+
# GH 46889
148+
ser = Series(["foo", "bar"])
149+
shallow_copy = ser.rename({1: 9}, copy=False)
150+
ser[0] = "foobar"
151+
assert ser[0] == shallow_copy[0]
152+
assert ser[1] == shallow_copy[9]

0 commit comments

Comments
 (0)