Skip to content

GH1139 Series.rename inplace #1140

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 9 commits into from
Mar 7, 2025
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
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
15 changes: 13 additions & 2 deletions pandas-stubs/core/series.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -1041,9 +1041,20 @@ class Series(IndexOpsMixin[S1], NDFrame):
broadcast_axis: AxisIndex | None = ...,
) -> tuple[Series, Series]: ...
@overload
def rename(
def rename( # pyright: ignore[reportOverlappingOverload]
self,
index: Renamer | Hashable | None = ...,
index: Hashable = ...,
*,
axis: Axis | None = ...,
copy: bool = ...,
inplace: Literal[True],
level: Level | None = ...,
errors: IgnoreRaise = ...,
) -> Self: ...
@overload
def rename( # type: ignore[overload-cannot-match]
self,
index: Renamer | None = ...,
*,
axis: Axis | None = ...,
copy: bool = ...,
Expand Down
35 changes: 27 additions & 8 deletions tests/test_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
TimedeltaSeries: TypeAlias = pd.Series
TimestampSeries: TypeAlias = pd.Series
OffsetSeries: TypeAlias = pd.Series
ExtensionArray: TypeAlias = np.ndarray

if TYPE_CHECKING:
from pandas._typing import (
Expand Down Expand Up @@ -1137,7 +1138,6 @@ def test_types_set_flags() -> None:

def test_types_getitem() -> None:
s = pd.Series({"key": [0, 1, 2, 3]})
key: list[int] = s["key"]
s2 = pd.Series([0, 1, 2, 3])
check(assert_type(s2[0], int), np.integer)
check(assert_type(s[:2], pd.Series), pd.Series)
Expand Down Expand Up @@ -1180,12 +1180,28 @@ def test_types_rename_axis() -> None:


def test_types_values() -> None:
n1: np.ndarray | ExtensionArray = pd.Series([1, 2, 3]).values
n2: np.ndarray | ExtensionArray = pd.Series(list("aabc")).values
n3: np.ndarray | ExtensionArray = pd.Series(list("aabc")).astype("category").values
n4: np.ndarray | ExtensionArray = pd.Series(
pd.date_range("20130101", periods=3, tz="US/Eastern")
).values
check(
assert_type(pd.Series([1, 2, 3]).values, "np.ndarray | ExtensionArray"),
np.ndarray,
)
check(
assert_type(pd.Series(list("aabc")).values, " np.ndarray | ExtensionArray "),
np.ndarray,
)
check(
assert_type(
pd.Series(list("aabc")).astype("category").values,
"np.ndarray | ExtensionArray",
),
pd.Categorical,
)
check(
assert_type(
pd.Series(pd.date_range("20130101", periods=3, tz="US/Eastern")).values,
"np.ndarray | ExtensionArray",
),
np.ndarray,
)


def test_types_rename() -> None:
Expand All @@ -1212,7 +1228,10 @@ def add1(x: int) -> int:
check(assert_type(s5, "pd.Series[int]"), pd.Series, np.integer)
# inplace
# TODO fix issue with inplace=True returning a Series, cf pandas #60942
s6: None = pd.Series([1, 2, 3]).rename("A", inplace=True)
check(
assert_type(pd.Series([1, 2, 3]).rename("A", inplace=True), "pd.Series[int]"),
pd.Series,
)

if TYPE_CHECKING_INVALID_USAGE:
s7 = pd.Series([1, 2, 3]).rename({1: [3, 4, 5]}) # type: ignore[arg-type] # pyright: ignore[reportArgumentType]
Expand Down