Skip to content

Commit 40ecf6e

Browse files
committed
make rename compatible with parent
1 parent 526d52f commit 40ecf6e

File tree

3 files changed

+29
-7
lines changed

3 files changed

+29
-7
lines changed

pandas/core/groupby/groupby.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ class providing the base-class of operations.
2828
Sequence,
2929
TypeVar,
3030
Union,
31+
cast,
3132
)
3233

3334
import numpy as np
@@ -1698,7 +1699,7 @@ def size(self) -> FrameOrSeriesUnion:
16981699
result = self._obj_1d_constructor(result)
16991700

17001701
if not self.as_index:
1701-
result = result.rename("size").reset_index()
1702+
result = cast(Series, result.rename("size")).reset_index()
17021703

17031704
return self._reindex_output(result, fill_value=0)
17041705

pandas/core/series.py

+14-6
Original file line numberDiff line numberDiff line change
@@ -4345,14 +4345,16 @@ def align(
43454345

43464346
def rename(
43474347
self,
4348-
index=None,
4348+
mapper=None,
43494349
*,
4350+
index=None,
4351+
columns=None,
43504352
axis=None,
43514353
copy=True,
43524354
inplace=False,
43534355
level=None,
43544356
errors="ignore",
4355-
):
4357+
) -> Series | None:
43564358
"""
43574359
Alter Series index labels or name.
43584360
@@ -4368,7 +4370,7 @@ def rename(
43684370
----------
43694371
axis : {0 or "index"}
43704372
Unused. Accepted for compatibility with DataFrame method only.
4371-
index : scalar, hashable sequence, dict-like or function, optional
4373+
mapper : scalar, hashable sequence, dict-like or function, optional
43724374
Functions or dict-like are transformations to apply to
43734375
the index.
43744376
Scalar or hashable sequence-like will alter the ``Series.name``
@@ -4412,12 +4414,18 @@ def rename(
44124414
5 3
44134415
dtype: int64
44144416
"""
4415-
if callable(index) or is_dict_like(index):
4417+
if index is not None and mapper is not None:
4418+
raise TypeError("Cannot specify both 'mapper' and 'index'")
4419+
if index is None and mapper is None:
4420+
raise TypeError("Must pass a mapper")
4421+
if mapper is None:
4422+
mapper = index
4423+
if callable(mapper) or is_dict_like(mapper):
44164424
return super().rename(
4417-
index, copy=copy, inplace=inplace, level=level, errors=errors
4425+
mapper, copy=copy, inplace=inplace, level=level, errors=errors
44184426
)
44194427
else:
4420-
return self._set_name(index, inplace=inplace)
4428+
return self._set_name(mapper, inplace=inplace)
44214429

44224430
@overload
44234431
def set_axis(

pandas/tests/series/methods/test_rename.py

+13
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from datetime import datetime
22

33
import numpy as np
4+
import pytest
45

56
from pandas import (
67
Index,
@@ -101,3 +102,15 @@ def test_rename_callable(self):
101102
tm.assert_series_equal(result, expected)
102103

103104
assert result.name == expected.name
105+
106+
def test_rename_method_and_index(self):
107+
# GH 40977
108+
s = Series([1, 2])
109+
with pytest.raises(TypeError, match="Cannot specify both 'mapper' and 'index'"):
110+
s.rename(str, index=str)
111+
112+
def test_rename_no_method_no_index(self):
113+
# GH 40977
114+
s = Series([1, 2])
115+
with pytest.raises(TypeError, match="Must pass a mapper"):
116+
s.rename(inplace=False)

0 commit comments

Comments
 (0)