Skip to content

Commit ed5a004

Browse files
authored
Deprecate mapper argument in rename (#44672)
1 parent bb50531 commit ed5a004

File tree

4 files changed

+10
-22
lines changed

4 files changed

+10
-22
lines changed

pandas/core/frame.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -5079,7 +5079,7 @@ def rename(
50795079
2 2 5
50805080
4 3 6
50815081
"""
5082-
return super().rename(
5082+
return super()._rename(
50835083
mapper=mapper,
50845084
index=index,
50855085
columns=columns,

pandas/core/generic.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -989,7 +989,7 @@ def squeeze(self, axis=None):
989989
# ----------------------------------------------------------------------
990990
# Rename
991991

992-
def rename(
992+
def _rename(
993993
self: NDFrameT,
994994
mapper: Renamer | None = None,
995995
*,
@@ -4410,7 +4410,7 @@ def add_prefix(self: NDFrameT, prefix: str) -> NDFrameT:
44104410
# expected "NDFrameT")
44114411
# error: Argument 1 to "rename" of "NDFrame" has incompatible type
44124412
# "**Dict[str, partial[str]]"; expected "Union[str, int, None]"
4413-
return self.rename(**mapper) # type: ignore[return-value, arg-type]
4413+
return self._rename(**mapper) # type: ignore[return-value, arg-type]
44144414

44154415
@final
44164416
def add_suffix(self: NDFrameT, suffix: str) -> NDFrameT:
@@ -4474,7 +4474,7 @@ def add_suffix(self: NDFrameT, suffix: str) -> NDFrameT:
44744474
# expected "NDFrameT")
44754475
# error: Argument 1 to "rename" of "NDFrame" has incompatible type
44764476
# "**Dict[str, partial[str]]"; expected "Union[str, int, None]"
4477-
return self.rename(**mapper) # type: ignore[return-value, arg-type]
4477+
return self._rename(**mapper) # type: ignore[return-value, arg-type]
44784478

44794479
def sort_values(
44804480
self,

pandas/core/series.py

+6-12
Original file line numberDiff line numberDiff line change
@@ -4477,10 +4477,8 @@ def align(
44774477

44784478
def rename(
44794479
self,
4480-
mapper=None,
4481-
*,
44824480
index=None,
4483-
columns=None,
4481+
*,
44844482
axis=None,
44854483
copy=True,
44864484
inplace=False,
@@ -4502,7 +4500,7 @@ def rename(
45024500
----------
45034501
axis : {0 or "index"}
45044502
Unused. Accepted for compatibility with DataFrame method only.
4505-
mapper : scalar, hashable sequence, dict-like or function, optional
4503+
index : scalar, hashable sequence, dict-like or function, optional
45064504
Functions or dict-like are transformations to apply to
45074505
the index.
45084506
Scalar or hashable sequence-like will alter the ``Series.name``
@@ -4550,16 +4548,12 @@ def rename(
45504548
# Make sure we raise if an invalid 'axis' is passed.
45514549
axis = self._get_axis_number(axis)
45524550

4553-
if index is not None and mapper is not None:
4554-
raise TypeError("Cannot specify both 'mapper' and 'index'")
4555-
if mapper is None:
4556-
mapper = index
4557-
if callable(mapper) or is_dict_like(mapper):
4558-
return super().rename(
4559-
mapper, copy=copy, inplace=inplace, level=level, errors=errors
4551+
if callable(index) or is_dict_like(index):
4552+
return super()._rename(
4553+
index, copy=copy, inplace=inplace, level=level, errors=errors
45604554
)
45614555
else:
4562-
return self._set_name(mapper, inplace=inplace)
4556+
return self._set_name(index, inplace=inplace)
45634557

45644558
@overload
45654559
def set_axis(

pandas/tests/series/methods/test_rename.py

-6
Original file line numberDiff line numberDiff line change
@@ -105,12 +105,6 @@ def test_rename_callable(self):
105105

106106
assert result.name == expected.name
107107

108-
def test_rename_method_and_index(self):
109-
# GH 40977
110-
ser = Series([1, 2])
111-
with pytest.raises(TypeError, match="Cannot specify both 'mapper' and 'index'"):
112-
ser.rename(str, index=str)
113-
114108
def test_rename_none(self):
115109
# GH 40977
116110
ser = Series([1, 2], name="foo")

0 commit comments

Comments
 (0)