Skip to content

Commit 4f06971

Browse files
authored
TYP: Signature of "rename" incompatible with supertype "NDFrame" (#40979)
1 parent d6723fd commit 4f06971

File tree

5 files changed

+32
-12
lines changed

5 files changed

+32
-12
lines changed

pandas/core/frame.py

-4
Original file line numberDiff line numberDiff line change
@@ -5073,10 +5073,6 @@ def drop(
50735073
errors=errors,
50745074
)
50755075

5076-
@rewrite_axis_style_signature(
5077-
"mapper",
5078-
[("copy", True), ("inplace", False), ("level", None), ("errors", "ignore")],
5079-
)
50805076
def rename(
50815077
self,
50825078
mapper: Renamer | None = None,

pandas/core/groupby/groupby.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -2082,7 +2082,8 @@ def size(self) -> DataFrame | Series:
20822082
result = self._obj_1d_constructor(result)
20832083

20842084
if not self.as_index:
2085-
result = result.rename("size").reset_index()
2085+
# Item "None" of "Optional[Series]" has no attribute "reset_index"
2086+
result = result.rename("size").reset_index() # type: ignore[union-attr]
20862087

20872088
return self._reindex_output(result, fill_value=0)
20882089

pandas/core/series.py

+12-6
Original file line numberDiff line numberDiff line change
@@ -4468,14 +4468,16 @@ def align(
44684468

44694469
def rename(
44704470
self,
4471-
index=None,
4471+
mapper=None,
44724472
*,
4473+
index=None,
4474+
columns=None,
44734475
axis=None,
44744476
copy=True,
44754477
inplace=False,
44764478
level=None,
44774479
errors="ignore",
4478-
):
4480+
) -> Series | None:
44794481
"""
44804482
Alter Series index labels or name.
44814483
@@ -4491,7 +4493,7 @@ def rename(
44914493
----------
44924494
axis : {0 or "index"}
44934495
Unused. Accepted for compatibility with DataFrame method only.
4494-
index : scalar, hashable sequence, dict-like or function, optional
4496+
mapper : scalar, hashable sequence, dict-like or function, optional
44954497
Functions or dict-like are transformations to apply to
44964498
the index.
44974499
Scalar or hashable sequence-like will alter the ``Series.name``
@@ -4539,12 +4541,16 @@ def rename(
45394541
# Make sure we raise if an invalid 'axis' is passed.
45404542
axis = self._get_axis_number(axis)
45414543

4542-
if callable(index) or is_dict_like(index):
4544+
if index is not None and mapper is not None:
4545+
raise TypeError("Cannot specify both 'mapper' and 'index'")
4546+
if mapper is None:
4547+
mapper = index
4548+
if callable(mapper) or is_dict_like(mapper):
45434549
return super().rename(
4544-
index, copy=copy, inplace=inplace, level=level, errors=errors
4550+
mapper, copy=copy, inplace=inplace, level=level, errors=errors
45454551
)
45464552
else:
4547-
return self._set_name(index, inplace=inplace)
4553+
return self._set_name(mapper, inplace=inplace)
45484554

45494555
@overload
45504556
def set_axis(

pandas/io/json/_normalize.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -517,7 +517,11 @@ def _recursive_extract(data, path, seen_meta, level=0):
517517
result = DataFrame(records)
518518

519519
if record_prefix is not None:
520-
result = result.rename(columns=lambda x: f"{record_prefix}{x}")
520+
# Incompatible types in assignment (expression has type "Optional[DataFrame]",
521+
# variable has type "DataFrame")
522+
result = result.rename( # type: ignore[assignment]
523+
columns=lambda x: f"{record_prefix}{x}"
524+
)
521525

522526
# Data types, a problem
523527
for k, v in meta_vals.items():

pandas/tests/series/methods/test_rename.py

+13
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,19 @@ 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+
114+
def test_rename_none(self):
115+
# GH 40977
116+
ser = Series([1, 2], name="foo")
117+
result = ser.rename(None)
118+
expected = Series([1, 2])
119+
tm.assert_series_equal(result, expected)
120+
108121
def test_rename_series_with_multiindex(self):
109122
# issue #43659
110123
arrays = [

0 commit comments

Comments
 (0)