diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 76f68fdaa7845..803d1c914c954 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -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, diff --git a/pandas/core/groupby/groupby.py b/pandas/core/groupby/groupby.py index faa32b31a73d7..1dafe1618a9f8 100644 --- a/pandas/core/groupby/groupby.py +++ b/pandas/core/groupby/groupby.py @@ -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) diff --git a/pandas/core/series.py b/pandas/core/series.py index 03fac7cceabb7..d5909b8659903 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -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. @@ -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`` @@ -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( diff --git a/pandas/io/json/_normalize.py b/pandas/io/json/_normalize.py index 2c2c127394fb6..36a7949a9f1e3 100644 --- a/pandas/io/json/_normalize.py +++ b/pandas/io/json/_normalize.py @@ -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(): diff --git a/pandas/tests/series/methods/test_rename.py b/pandas/tests/series/methods/test_rename.py index a78abfa63cff4..3425dd8f019e7 100644 --- a/pandas/tests/series/methods/test_rename.py +++ b/pandas/tests/series/methods/test_rename.py @@ -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 = [