Skip to content

GH804 Fix property setter/getter for Index.names #1308

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 3 commits into from
Aug 7, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
7 changes: 4 additions & 3 deletions pandas-stubs/core/indexes/base.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ from pandas._typing import (
MaskType,
NaPosition,
ReindexMethod,
SequenceNotStr,
SliceType,
TimedeltaDtypeArg,
TimestampDtypeArg,
Expand Down Expand Up @@ -306,13 +307,13 @@ class Index(IndexOpsMixin[S1]):
def to_series(self, index=..., name: Hashable = ...) -> Series: ...
def to_frame(self, index: bool = ..., name=...) -> DataFrame: ...
@property
def name(self) -> Hashable | None: ...
def name(self) -> _str | None: ...
@name.setter
def name(self, value) -> None: ...
@property
def names(self) -> list[Hashable]: ...
def names(self) -> list[_str | None]: ...
@names.setter
def names(self, names: Sequence[Hashable]) -> None: ...
def names(self, names: SequenceNotStr[_str | None]) -> None: ...
Copy link
Collaborator

Choose a reason for hiding this comment

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

All of these should use Hashable instead of _str

e.g., the following is valid:

df.index.names=[3]

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes I realized this morning, all fixed with new test case.

def set_names(self, names, *, level=..., inplace: bool = ...): ...
@overload
def rename(self, name, *, inplace: Literal[False] = False) -> Self: ...
Expand Down
18 changes: 18 additions & 0 deletions tests/test_indexes.py
Original file line number Diff line number Diff line change
Expand Up @@ -1435,3 +1435,21 @@ def test_multiindex_range() -> None:
[range(3), pd.Series([2, 3, 5])],
)
check(assert_type(midx_mixed_types, pd.MultiIndex), pd.MultiIndex)


def test_index_naming() -> None:
"""
Test index names type both for the getter and the setter.
The names of an index should be settable with a sequence (not str) and names
property is a list[str | None] (FrozenList).
"""
df = pd.DataFrame({"a": ["a", "b", "c"], "i": [10, 11, 12]})

df.index.names = ["idx"]
check(assert_type(df.index.names, list[str | None]), list)
df.index.names = ("idx2",)
check(assert_type(df.index.names, list[str | None]), list)
df.index.names = [None]
check(assert_type(df.index.names, list[str | None]), list)
df.index.names = (None,)
check(assert_type(df.index.names, list[str | None]), list)
Loading