Skip to content

Deprecate mapper argument in rename #44672

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Dec 5, 2021
Merged
2 changes: 1 addition & 1 deletion pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -5192,7 +5192,7 @@ def rename(
2 2 5
4 3 6
"""
return super().rename(
return super()._rename(
mapper=mapper,
index=index,
columns=columns,
Expand Down
6 changes: 3 additions & 3 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -989,7 +989,7 @@ def squeeze(self, axis=None):
# ----------------------------------------------------------------------
# Rename

def rename(
def _rename(
self: NDFrameT,
mapper: Renamer | None = None,
*,
Expand Down Expand Up @@ -4418,7 +4418,7 @@ def add_prefix(self: NDFrameT, prefix: str) -> NDFrameT:
# expected "NDFrameT")
# error: Argument 1 to "rename" of "NDFrame" has incompatible type
# "**Dict[str, partial[str]]"; expected "Union[str, int, None]"
return self.rename(**mapper) # type: ignore[return-value, arg-type]
return self._rename(**mapper) # type: ignore[return-value, arg-type]

@final
def add_suffix(self: NDFrameT, suffix: str) -> NDFrameT:
Expand Down Expand Up @@ -4482,7 +4482,7 @@ def add_suffix(self: NDFrameT, suffix: str) -> NDFrameT:
# expected "NDFrameT")
# error: Argument 1 to "rename" of "NDFrame" has incompatible type
# "**Dict[str, partial[str]]"; expected "Union[str, int, None]"
return self.rename(**mapper) # type: ignore[return-value, arg-type]
return self._rename(**mapper) # type: ignore[return-value, arg-type]

def sort_values(
self,
Expand Down
18 changes: 6 additions & 12 deletions pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -4468,10 +4468,8 @@ def align(

def rename(
self,
mapper=None,
*,
index=None,
columns=None,
*,
axis=None,
copy=True,
inplace=False,
Expand All @@ -4493,7 +4491,7 @@ def rename(
----------
axis : {0 or "index"}
Unused. Accepted for compatibility with DataFrame method only.
mapper : 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``
Expand Down Expand Up @@ -4541,16 +4539,12 @@ def rename(
# Make sure we raise if an invalid 'axis' is passed.
axis = self._get_axis_number(axis)

if index is not None and mapper is not None:
raise TypeError("Cannot specify both 'mapper' and 'index'")
if mapper is None:
mapper = index
if callable(mapper) or is_dict_like(mapper):
return super().rename(
mapper, copy=copy, inplace=inplace, level=level, errors=errors
if callable(index) or is_dict_like(index):
return super()._rename(
index, copy=copy, inplace=inplace, level=level, errors=errors
)
else:
return self._set_name(mapper, inplace=inplace)
return self._set_name(index, inplace=inplace)

@overload
def set_axis(
Expand Down
6 changes: 0 additions & 6 deletions pandas/tests/series/methods/test_rename.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,6 @@ def test_rename_callable(self):

assert result.name == expected.name

def test_rename_method_and_index(self):
# GH 40977
ser = Series([1, 2])
with pytest.raises(TypeError, match="Cannot specify both 'mapper' and 'index'"):
ser.rename(str, index=str)

def test_rename_none(self):
# GH 40977
ser = Series([1, 2], name="foo")
Expand Down