Skip to content

GH935 Create overload for pd.Index.rename #1012

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 1 commit into from
Oct 8, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
5 changes: 4 additions & 1 deletion pandas-stubs/core/indexes/base.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,10 @@ class Index(IndexOpsMixin[S1]):
@names.setter
def names(self, names: list[_str]): ...
def set_names(self, names, *, level=..., inplace: bool = ...): ...
def rename(self, name, inplace: bool = ...) -> Self: ...
@overload
def rename(self, name, inplace: Literal[False] = False) -> Self: ...
@overload
def rename(self, name, inplace: Literal[True]) -> None: ...
@property
def nlevels(self) -> int: ...
def sortlevel(self, level=..., ascending: bool = ..., sort_remaining=...): ...
Expand Down
9 changes: 9 additions & 0 deletions tests/test_indexes.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,20 @@ def test_str_match() -> None:


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


def test_index_rename_inplace() -> None:
"""Test that index rename in-place does not return anything (None)."""
ind = pd.Index([1, 2, 3], name="foo")
ind2 = ind.rename("goo", inplace=True)
check(assert_type(ind2, None), type(None))
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not totally sure about the structure when there is a None return, will double check with another example from the code.

assert ind2 is None


def test_index_dropna():
idx = pd.Index([1, 2])

Expand Down
Loading