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 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
7 changes: 4 additions & 3 deletions pandas-stubs/core/indexes/base.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ from pandas._typing import (
NaPosition,
ReindexMethod,
Scalar,
SequenceNotStr,
SliceType,
TimedeltaDtypeArg,
TimestampDtypeArg,
Expand Down Expand Up @@ -316,11 +317,11 @@ class Index(IndexOpsMixin[S1]):
@property
def name(self) -> Hashable | None: ...
@name.setter
def name(self, value) -> None: ...
def name(self, value: Hashable) -> None: ...
@property
def names(self) -> list[Hashable]: ...
def names(self) -> list[Hashable | None]: ...
@names.setter
def names(self, names: Sequence[Hashable]) -> None: ...
def names(self, names: SequenceNotStr[Hashable | None]) -> None: ...
def set_names(self, names, *, level=..., inplace: bool = ...): ...
@overload
def rename(self, name, *, inplace: Literal[False] = False) -> Self: ...
Expand Down
21 changes: 21 additions & 0 deletions tests/test_indexes.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

from collections.abc import Hashable
import datetime as dt
from typing import (
TYPE_CHECKING,
Expand Down Expand Up @@ -1433,3 +1434,23 @@ 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[Hashable | None] (FrozenList).
"""
df = pd.DataFrame({"a": ["a", "b", "c"], "i": [10, 11, 12]})

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