From fb8df43d74495bebc924cef3c89079e88d6c0663 Mon Sep 17 00:00:00 2001 From: th3nn3ss Date: Thu, 11 Aug 2022 08:05:21 +0100 Subject: [PATCH 1/6] add documentation to series rename func for args available to it --- pandas/core/series.py | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/pandas/core/series.py b/pandas/core/series.py index 206fcbe05d006..763fc22dbe07e 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -4866,10 +4866,15 @@ def rename( 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. + 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 `index` contains labels not found in axis. + If 'ignore', existing keys will be renamed and extra keys will be ignored. Returns ------- From 2e618eaccb43e7ff7422b4a30bf4c0856362f0c5 Mon Sep 17 00:00:00 2001 From: th3nn3ss Date: Thu, 11 Aug 2022 12:40:41 +0100 Subject: [PATCH 2/6] add tests for rename error and copy arguments --- pandas/core/series.py | 2 +- pandas/tests/series/methods/test_rename.py | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/pandas/core/series.py b/pandas/core/series.py index 763fc22dbe07e..83c3e43f00cb0 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -4873,7 +4873,7 @@ def rename( 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 `index` contains labels not found in axis. + 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] From fe7da63e6317ae0292d6b6f965b3b19be82a7c77 Mon Sep 17 00:00:00 2001 From: th3nn3ss Date: Thu, 11 Aug 2022 12:54:02 +0100 Subject: [PATCH 3/6] fix PEP 8 issues 'line too long' --- pandas/core/series.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pandas/core/series.py b/pandas/core/series.py index 83c3e43f00cb0..206bd64d465a6 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -4873,7 +4873,8 @@ def rename( 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 '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 From 3b27ccc8a6f58fded28136e433030d5d9013646e Mon Sep 17 00:00:00 2001 From: th3nn3ss Date: Thu, 11 Aug 2022 12:58:56 +0100 Subject: [PATCH 4/6] fix whitespace --- pandas/core/series.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/core/series.py b/pandas/core/series.py index 206bd64d465a6..7ecbdbc526a78 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -4873,7 +4873,7 @@ def rename( 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 + 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. From 0e5ab496cab73bafded48dd87253b9219686f21d Mon Sep 17 00:00:00 2001 From: th3nn3ss Date: Thu, 11 Aug 2022 13:56:39 +0100 Subject: [PATCH 5/6] fix doc string parameter order --- pandas/core/series.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/pandas/core/series.py b/pandas/core/series.py index 7ecbdbc526a78..ff9ca816ac058 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -4859,13 +4859,13 @@ def rename( Parameters ---------- - axis : {0 or 'index'} - Unused. Parameter needed for compatibility with DataFrame. - index : scalar, hashable sequence, dict-like or function, optional - Functions or dict-like are transformations to apply to + 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. + axis : {0 or 'index'} + Unused. Parameter needed for compatibility with DataFrame. copy : bool, default True Also copy underlying data. inplace : bool, default False From 44b6356788b791147bced613e5c8e388b7d0b704 Mon Sep 17 00:00:00 2001 From: th3nn3ss Date: Thu, 11 Aug 2022 15:05:31 +0100 Subject: [PATCH 6/6] fix doc string --- pandas/core/series.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pandas/core/series.py b/pandas/core/series.py index ff9ca816ac058..c96a5c7f15e71 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -4859,8 +4859,8 @@ def rename( Parameters ---------- - index : scalar, hashable sequence, dict-like or function - optional Functions or dict-like are transformations to apply to + 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.