diff --git a/pandas/core/series.py b/pandas/core/series.py index 206fcbe05d006..c96a5c7f15e71 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -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 ------- diff --git a/pandas/tests/series/methods/test_rename.py b/pandas/tests/series/methods/test_rename.py index 90c8f775586e6..729c07b8bdde7 100644 --- a/pandas/tests/series/methods/test_rename.py +++ b/pandas/tests/series/methods/test_rename.py @@ -1,4 +1,5 @@ from datetime import datetime +import re import numpy as np import pytest @@ -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]