Skip to content

Commit 668292f

Browse files
GH935 Create overload for pd.Index.rename (#1012)
1 parent e50358f commit 668292f

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

pandas-stubs/core/indexes/base.pyi

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,10 @@ class Index(IndexOpsMixin[S1]):
292292
@names.setter
293293
def names(self, names: list[_str]): ...
294294
def set_names(self, names, *, level=..., inplace: bool = ...): ...
295-
def rename(self, name, inplace: bool = ...) -> Self: ...
295+
@overload
296+
def rename(self, name, inplace: Literal[False] = False) -> Self: ...
297+
@overload
298+
def rename(self, name, inplace: Literal[True]) -> None: ...
296299
@property
297300
def nlevels(self) -> int: ...
298301
def sortlevel(self, level=..., ascending: bool = ..., sort_remaining=...): ...

tests/test_indexes.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,11 +121,20 @@ def test_str_match() -> None:
121121

122122

123123
def test_index_rename() -> None:
124+
"""Test that index rename returns an element of type Index."""
124125
ind = pd.Index([1, 2, 3], name="foo")
125126
ind2 = ind.rename("goo")
126127
check(assert_type(ind2, "pd.Index[int]"), pd.Index, np.integer)
127128

128129

130+
def test_index_rename_inplace() -> None:
131+
"""Test that index rename in-place does not return anything (None)."""
132+
ind = pd.Index([1, 2, 3], name="foo")
133+
ind2 = ind.rename("goo", inplace=True)
134+
check(assert_type(ind2, None), type(None))
135+
assert ind2 is None
136+
137+
129138
def test_index_dropna():
130139
idx = pd.Index([1, 2])
131140

0 commit comments

Comments
 (0)