Skip to content

TYP: Signature of "rename" incompatible with supertype "NDFrame" #40979

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 18 commits into from
Nov 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
40ecf6e
make rename compatible with parent
MarcoGorelli Apr 16, 2021
ad1f65c
allow renaming with None
MarcoGorelli Apr 16, 2021
b173fc4
Merge remote-tracking branch 'upstream/master' into rename-compatibility
MarcoGorelli Apr 17, 2021
77f4bdc
cast -> type: ignore
MarcoGorelli Apr 17, 2021
b249ee4
remove rewrite_axis_style_signature
MarcoGorelli Apr 25, 2021
c083ce8
Merge remote-tracking branch 'upstream/master' into rename-compatibility
MarcoGorelli Apr 25, 2021
4ffdb3f
ignore error (will be fixed later w/overload)
MarcoGorelli Apr 25, 2021
0a6baf8
ignore error (will be fixed later w/overload)
MarcoGorelli Apr 25, 2021
b8fef00
Merge remote-tracking branch 'upstream/master' into rename-compatibility
MarcoGorelli Apr 25, 2021
90dc046
Merge remote-tracking branch 'upstream/master' into rename-compatibility
MarcoGorelli Apr 27, 2021
d027e6d
s -> ser (avoid one-letter name)
MarcoGorelli Apr 28, 2021
786d320
Merge remote-tracking branch 'upstream/master' into rename-compatibility
MarcoGorelli Jun 3, 2021
fbff66c
Merge remote-tracking branch 'upstream/master' into rename-compatibility
MarcoGorelli Aug 8, 2021
9277fe2
Merge remote-tracking branch 'upstream/master' into rename-compatibility
MarcoGorelli Oct 11, 2021
d6856ad
Merge remote-tracking branch 'upstream/master' into rename-compatibility
MarcoGorelli Nov 7, 2021
98959d0
Merge remote-tracking branch 'upstream/master' into rename-compatibility
MarcoGorelli Nov 16, 2021
6e5bba6
Merge remote-tracking branch 'upstream/master' into rename-compatibility
MarcoGorelli Nov 25, 2021
2ac76d0
Merge remote-tracking branch 'upstream/master' into rename-compatibility
MarcoGorelli Nov 26, 2021
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
4 changes: 0 additions & 4 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -5073,10 +5073,6 @@ def drop(
errors=errors,
)

@rewrite_axis_style_signature(
"mapper",
[("copy", True), ("inplace", False), ("level", None), ("errors", "ignore")],
)
def rename(
self,
mapper: Renamer | None = None,
Expand Down
3 changes: 2 additions & 1 deletion pandas/core/groupby/groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -2082,7 +2082,8 @@ def size(self) -> DataFrame | Series:
result = self._obj_1d_constructor(result)

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

return self._reindex_output(result, fill_value=0)

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

def rename(
self,
index=None,
mapper=None,
*,
index=None,
columns=None,
axis=None,
copy=True,
inplace=False,
level=None,
errors="ignore",
):
) -> Series | None:
"""
Alter Series index labels or name.

Expand All @@ -4491,7 +4493,7 @@ def rename(
----------
axis : {0 or "index"}
Unused. Accepted for compatibility with DataFrame method only.
index : scalar, hashable sequence, dict-like or function, optional
mapper : 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 @@ -4539,12 +4541,16 @@ def rename(
# Make sure we raise if an invalid 'axis' is passed.
axis = self._get_axis_number(axis)

if callable(index) or is_dict_like(index):
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(
index, copy=copy, inplace=inplace, level=level, errors=errors
mapper, copy=copy, inplace=inplace, level=level, errors=errors
)
else:
return self._set_name(index, inplace=inplace)
return self._set_name(mapper, inplace=inplace)

@overload
def set_axis(
Expand Down
6 changes: 5 additions & 1 deletion pandas/io/json/_normalize.py
Original file line number Diff line number Diff line change
Expand Up @@ -517,7 +517,11 @@ def _recursive_extract(data, path, seen_meta, level=0):
result = DataFrame(records)

if record_prefix is not None:
result = result.rename(columns=lambda x: f"{record_prefix}{x}")
# Incompatible types in assignment (expression has type "Optional[DataFrame]",
# variable has type "DataFrame")
result = result.rename( # type: ignore[assignment]
columns=lambda x: f"{record_prefix}{x}"
)

# Data types, a problem
for k, v in meta_vals.items():
Expand Down
13 changes: 13 additions & 0 deletions pandas/tests/series/methods/test_rename.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,19 @@ 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")
result = ser.rename(None)
expected = Series([1, 2])
tm.assert_series_equal(result, expected)

def test_rename_series_with_multiindex(self):
# issue #43659
arrays = [
Expand Down