Skip to content

Commit 6a64b60

Browse files
lgmsantosgaluhsahid
authored andcommitted
BUG: Series.rename raises error on values accepted by Series construc… (pandas-dev#27814)
* BUG: Series.rename raises error on values accepted by Series constructor.
1 parent d436728 commit 6a64b60

File tree

3 files changed

+23
-5
lines changed

3 files changed

+23
-5
lines changed

doc/source/whatsnew/v0.25.1.rst

+1
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ Other
108108
^^^^^
109109

110110
- Bug in :meth:`Series.replace` and :meth:`DataFrame.replace` when replacing timezone-aware timestamps using a dict-like replacer (:issue:`27720`)
111+
- 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`)
111112

112113
.. _whatsnew_0.251.contributors:
113114

pandas/core/series.py

+3-5
Original file line numberDiff line numberDiff line change
@@ -4165,12 +4165,10 @@ def rename(self, index=None, **kwargs):
41654165
"""
41664166
kwargs["inplace"] = validate_bool_kwarg(kwargs.get("inplace", False), "inplace")
41674167

4168-
non_mapping = is_scalar(index) or (
4169-
is_list_like(index) and not is_dict_like(index)
4170-
)
4171-
if non_mapping:
4168+
if callable(index) or is_dict_like(index):
4169+
return super().rename(index=index, **kwargs)
4170+
else:
41724171
return self._set_name(index, inplace=kwargs.get("inplace"))
4173-
return super().rename(index=index, **kwargs)
41744172

41754173
@Substitution(**_shared_doc_kwargs)
41764174
@Appender(generic.NDFrame.reindex.__doc__)

pandas/tests/series/test_alter_axes.py

+19
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,25 @@ def test_rename_axis_none(self, kwargs):
267267
expected = Series([1, 2, 3], index=expected_index)
268268
tm.assert_series_equal(result, expected)
269269

270+
def test_rename_with_custom_indexer(self):
271+
# GH 27814
272+
class MyIndexer:
273+
pass
274+
275+
ix = MyIndexer()
276+
s = Series([1, 2, 3]).rename(ix)
277+
assert s.name is ix
278+
279+
def test_rename_with_custom_indexer_inplace(self):
280+
# GH 27814
281+
class MyIndexer:
282+
pass
283+
284+
ix = MyIndexer()
285+
s = Series([1, 2, 3])
286+
s.rename(ix, inplace=True)
287+
assert s.name is ix
288+
270289
def test_set_axis_inplace_axes(self, axis_series):
271290
# GH14636
272291
ser = Series(np.arange(4), index=[1, 3, 5, 7], dtype="int64")

0 commit comments

Comments
 (0)