Skip to content

Commit 64575f6

Browse files
GH1139 Series.rename inplace
1 parent 4b42588 commit 64575f6

File tree

3 files changed

+42
-12
lines changed

3 files changed

+42
-12
lines changed

pandas-stubs/core/indexes/base.pyi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ class Index(IndexOpsMixin[S1]):
6767
__hash__: ClassVar[None] # type: ignore[assignment]
6868
# overloads with additional dtypes
6969
@overload
70-
def __new__(
70+
def __new__( # pyright: ignore[reportOverlappingOverload]
7171
cls,
7272
data: Sequence[int | np.integer] | IndexOpsMixin[int] | np_ndarray_anyint,
7373
*,

pandas-stubs/core/series.pyi

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ class Series(IndexOpsMixin[S1], NDFrame):
249249
copy: bool = ...,
250250
) -> Series[float]: ...
251251
@overload
252-
def __new__( # type: ignore[overload-overlap]
252+
def __new__( # type: ignore[overload-overlap] # pyright: ignore[reportOverlappingOverload]
253253
cls,
254254
data: Sequence[Never],
255255
index: Axes | None = ...,
@@ -1041,9 +1041,20 @@ class Series(IndexOpsMixin[S1], NDFrame):
10411041
broadcast_axis: AxisIndex | None = ...,
10421042
) -> tuple[Series, Series]: ...
10431043
@overload
1044-
def rename(
1044+
def rename( # pyright: ignore[reportOverlappingOverload]
10451045
self,
1046-
index: Renamer | Hashable | None = ...,
1046+
index: Hashable = ...,
1047+
*,
1048+
axis: Axis | None = ...,
1049+
copy: bool = ...,
1050+
inplace: Literal[True],
1051+
level: Level | None = ...,
1052+
errors: IgnoreRaise = ...,
1053+
) -> Self: ...
1054+
@overload
1055+
def rename( # type: ignore[overload-cannot-match]
1056+
self,
1057+
index: Renamer | None = ...,
10471058
*,
10481059
axis: Axis | None = ...,
10491060
copy: bool = ...,

tests/test_series.py

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@
7070
TimedeltaSeries: TypeAlias = pd.Series
7171
TimestampSeries: TypeAlias = pd.Series
7272
OffsetSeries: TypeAlias = pd.Series
73+
ExtensionArray: TypeAlias = np.ndarray
7374

7475
if TYPE_CHECKING:
7576
from pandas._typing import (
@@ -1137,7 +1138,6 @@ def test_types_set_flags() -> None:
11371138

11381139
def test_types_getitem() -> None:
11391140
s = pd.Series({"key": [0, 1, 2, 3]})
1140-
key: list[int] = s["key"]
11411141
s2 = pd.Series([0, 1, 2, 3])
11421142
check(assert_type(s2[0], int), np.integer)
11431143
check(assert_type(s[:2], pd.Series), pd.Series)
@@ -1180,12 +1180,28 @@ def test_types_rename_axis() -> None:
11801180

11811181

11821182
def test_types_values() -> None:
1183-
n1: np.ndarray | ExtensionArray = pd.Series([1, 2, 3]).values
1184-
n2: np.ndarray | ExtensionArray = pd.Series(list("aabc")).values
1185-
n3: np.ndarray | ExtensionArray = pd.Series(list("aabc")).astype("category").values
1186-
n4: np.ndarray | ExtensionArray = pd.Series(
1187-
pd.date_range("20130101", periods=3, tz="US/Eastern")
1188-
).values
1183+
check(
1184+
assert_type(pd.Series([1, 2, 3]).values, "np.ndarray | ExtensionArray"),
1185+
np.ndarray,
1186+
)
1187+
check(
1188+
assert_type(pd.Series(list("aabc")).values, " np.ndarray | ExtensionArray "),
1189+
np.ndarray,
1190+
)
1191+
check(
1192+
assert_type(
1193+
pd.Series(list("aabc")).astype("category").values,
1194+
"np.ndarray | ExtensionArray",
1195+
),
1196+
pd.Categorical,
1197+
)
1198+
check(
1199+
assert_type(
1200+
pd.Series(pd.date_range("20130101", periods=3, tz="US/Eastern")).values,
1201+
"np.ndarray | ExtensionArray",
1202+
),
1203+
np.ndarray,
1204+
)
11891205

11901206

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

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

0 commit comments

Comments
 (0)