Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.25.1.rst
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ 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`)

.. _whatsnew_0.251.contributors:

Expand Down
8 changes: 3 additions & 5 deletions pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -4165,12 +4165,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__)
Expand Down
19 changes: 19 additions & 0 deletions pandas/tests/series/test_alter_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,25 @@ def test_rename_axis_none(self, kwargs):
expected = Series([1, 2, 3], index=expected_index)
tm.assert_series_equal(result, expected)

def test_rename_with_custom_indexer(self):
# GH 27814
class MyIndexer:
pass

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

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
ser = Series(np.arange(4), index=[1, 3, 5, 7], dtype="int64")
Expand Down