From 64a73bfe2e96676f88c322d7bc55af1b44a1f26b Mon Sep 17 00:00:00 2001 From: Luiz Gustavo Date: Wed, 7 Aug 2019 22:16:26 -0300 Subject: [PATCH 1/5] BUG: Series.rename raises error on values accepted by Series constructor. --- pandas/core/series.py | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/pandas/core/series.py b/pandas/core/series.py index 4e64a25e430eb..cebb8ed636da0 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -4164,12 +4164,10 @@ def rename(self, index=None, **kwargs): """ kwargs["inplace"] = validate_bool_kwarg(kwargs.get("inplace", False), "inplace") - non_mapping = is_scalar(index) or ( - is_list_like(index) and not is_dict_like(index) - ) - if non_mapping: + if callable(index) or is_dict_like(index): + return super().rename(index=index, **kwargs) + else: return self._set_name(index, inplace=kwargs.get("inplace")) - return super().rename(index=index, **kwargs) @Substitution(**_shared_doc_kwargs) @Appender(generic.NDFrame.reindex.__doc__) From 0d742fe79911bf65f14f60db8655d449ae273d79 Mon Sep 17 00:00:00 2001 From: Luiz Gustavo Date: Thu, 8 Aug 2019 10:27:41 -0300 Subject: [PATCH 2/5] TST: renaming Series with custom indexer #27814 --- pandas/tests/series/test_alter_axes.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/pandas/tests/series/test_alter_axes.py b/pandas/tests/series/test_alter_axes.py index 0a25d6ba203cb..bd3747b4ca55a 100644 --- a/pandas/tests/series/test_alter_axes.py +++ b/pandas/tests/series/test_alter_axes.py @@ -267,6 +267,19 @@ def test_rename_axis_none(self, kwargs): expected = Series([1, 2, 3], index=expected_index) tm.assert_series_equal(result, expected) + def test_rename_custom_indexer(self): + # GH 27814 + + class MyIndexer: pass + + ix1, ix2 = MyIndexer(), MyIndexer() + s = Series([1, 2, 3]) + s = s.rename(ix1) + assert s.name is ix1 + + s.rename(ix2, inplace=True) + assert s.name is ix2 + def test_set_axis_inplace_axes(self, axis_series): # GH14636 ser = Series(np.arange(4), index=[1, 3, 5, 7], dtype="int64") From 25ad7c8ac1a0c4cd2ce3a66a7edac6d6f8ce6937 Mon Sep 17 00:00:00 2001 From: Luiz Gustavo Date: Thu, 8 Aug 2019 10:36:03 -0300 Subject: [PATCH 3/5] CLN: pep8 compliance --- pandas/tests/series/test_alter_axes.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pandas/tests/series/test_alter_axes.py b/pandas/tests/series/test_alter_axes.py index bd3747b4ca55a..d6a9ade9ada2f 100644 --- a/pandas/tests/series/test_alter_axes.py +++ b/pandas/tests/series/test_alter_axes.py @@ -269,8 +269,8 @@ def test_rename_axis_none(self, kwargs): def test_rename_custom_indexer(self): # GH 27814 - - class MyIndexer: pass + class MyIndexer: + pass ix1, ix2 = MyIndexer(), MyIndexer() s = Series([1, 2, 3]) From 3519eef5dee60eb7e058976e0724e562e0e5e405 Mon Sep 17 00:00:00 2001 From: Luiz Gustavo Date: Thu, 8 Aug 2019 22:02:58 -0300 Subject: [PATCH 4/5] DOC: addition of bug-fix to whatsnew file --- doc/source/whatsnew/v0.25.1.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v0.25.1.rst b/doc/source/whatsnew/v0.25.1.rst index f1d3f152e503d..874d9caec4fc2 100644 --- a/doc/source/whatsnew/v0.25.1.rst +++ b/doc/source/whatsnew/v0.25.1.rst @@ -155,7 +155,7 @@ ExtensionArray Other ^^^^^ - Bug in :meth:`Series.replace` and :meth:`DataFrame.replace` when replacing timezone-aware timestamps using a dict-like replacer (:issue:`27720`) -- +- Bug in :meth:`Series.rename` when using a custom type indexer. Now any value that isn't callable or dict-like is treated as a scalar. (:issue:`27814`) - - From 5c751b965acfff9c0515fc4eac864f88cd3d2531 Mon Sep 17 00:00:00 2001 From: Luiz Gustavo Date: Sun, 11 Aug 2019 10:51:55 -0300 Subject: [PATCH 5/5] TST: split test case --- pandas/tests/series/test_alter_axes.py | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/pandas/tests/series/test_alter_axes.py b/pandas/tests/series/test_alter_axes.py index d6a9ade9ada2f..5d74ad95be90d 100644 --- a/pandas/tests/series/test_alter_axes.py +++ b/pandas/tests/series/test_alter_axes.py @@ -267,18 +267,24 @@ def test_rename_axis_none(self, kwargs): expected = Series([1, 2, 3], index=expected_index) tm.assert_series_equal(result, expected) - def test_rename_custom_indexer(self): + def test_rename_with_custom_indexer(self): # GH 27814 class MyIndexer: pass - ix1, ix2 = MyIndexer(), MyIndexer() - s = Series([1, 2, 3]) - s = s.rename(ix1) - assert s.name is ix1 + ix = MyIndexer() + s = Series([1, 2, 3]).rename(ix) + assert s.name is ix + + def test_rename_with_custom_indexer_inplace(self): + # GH 27814 + class MyIndexer: + pass - s.rename(ix2, inplace=True) - assert s.name is ix2 + ix = MyIndexer() + s = Series([1, 2, 3]) + s.rename(ix, inplace=True) + assert s.name is ix def test_set_axis_inplace_axes(self, axis_series): # GH14636