Skip to content
34 changes: 19 additions & 15 deletions pandas-stubs/core/indexes/base.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ from collections.abc import (
Callable,
Hashable,
Iterable,
MutableMapping,
Mapping,
Sequence,
)
from datetime import (
Expand Down Expand Up @@ -104,6 +104,7 @@ from pandas._typing import (
SequenceNotStr,
SliceType,
SupportsDType,
TakeIndexer,
TimedeltaDtypeArg,
TimestampDtypeArg,
np_1darray,
Expand Down Expand Up @@ -350,40 +351,37 @@ class Index(IndexOpsMixin[S1], ElementOpsMixin[S1]):
Index,
]: ...
@final
def is_(self, other) -> bool: ...
def is_(self, other: object) -> bool: ...
def __len__(self) -> int: ...
def __array__(
self, dtype: _str | np.dtype = ..., copy: bool | None = ...
) -> np_1darray: ...
def __array_wrap__(self, result, context=...): ...
@property
def dtype(self) -> DtypeObj: ...
@final
def ravel(self, order: _str = ...): ...
def ravel(self, order: _str = "C") -> Self: ...
def view(self, cls=...): ...
def astype(self, dtype: DtypeArg, copy: bool = True) -> Index: ...
def take(
self,
indices,
indices: TakeIndexer,
axis: int = 0,
allow_fill: bool = True,
fill_value: Scalar | None = None,
**kwargs: Any,
) -> Self: ...
def repeat(
self, repeats: int | AnyArrayLikeInt | Sequence[int], axis: None = None
): ...
def repeat(self, repeats, axis=...): ...
def copy(self, name: Hashable = ..., deep: bool = False) -> Self: ...
@final
def __copy__(self, **kwargs: Any): ...
@final
def __deepcopy__(self, memo: MutableMapping[int, Any] | None = None) -> Self: ...
def format(
self, name: bool = ..., formatter: Callable | None = ..., na_rep: _str = ...
) -> list[_str]: ...
def to_flat_index(self): ...
def to_flat_index(self) -> Index: ...
def to_series(
self, index: Index | None = None, name: Hashable | None = None
) -> Series[S1]: ...
def to_frame(self, index: bool = True, name=...) -> DataFrame: ...
def to_frame(self, index: bool = True, name: Hashable = ...) -> DataFrame: ...
@property
def name(self) -> Hashable | None: ...
@name.setter
Expand All @@ -392,11 +390,17 @@ class Index(IndexOpsMixin[S1], ElementOpsMixin[S1]):
def names(self) -> list[Hashable | None]: ...
@names.setter
def names(self, names: SequenceNotStr[Hashable | None]) -> None: ...
def set_names(self, names, *, level=..., inplace: bool = ...): ...
def set_names(
self,
names: Hashable | Sequence[Hashable] | Mapping[Any, Hashable],
*,
level: Level | Sequence[Level] | None = None,
inplace: bool = False,
) -> Self: ...
@overload
def rename(self, name, *, inplace: Literal[False] = False) -> Self: ...
def rename(self, name: Hashable, *, inplace: Literal[False] = False) -> Self: ...
@overload
def rename(self, name, *, inplace: Literal[True]) -> None: ...
def rename(self, name: Hashable, *, inplace: Literal[True]) -> None: ...
@property
def nlevels(self) -> int: ...
def get_level_values(self, level: int | _str) -> Index: ...
Expand Down
2 changes: 1 addition & 1 deletion pandas-stubs/core/indexes/multi.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ class MultiIndex(Index):
def dropna(self, how: AnyAll = "any") -> Self: ...
def get_level_values(self, level: str | int) -> Index: ...
def unique(self, level=...): ...
def to_frame(
def to_frame( # type: ignore[override] # pyright: ignore[reportIncompatibleMethodOverride]
self,
index: bool = True,
name: list[HashableT] = ...,
Expand Down
13 changes: 13 additions & 0 deletions tests/indexes/test_indexes.py
Original file line number Diff line number Diff line change
Expand Up @@ -1523,3 +1523,16 @@ def test_datetimeindex_where() -> None:

val_range = pd.RangeIndex(2).where(pd.Series([True, False]), 3)
check(assert_type(val_range, pd.Index), pd.RangeIndex)


def test_index_set_names() -> None:
"""Test Index.where with multiple types of other GH1419."""
idx = pd.Index([1, 2])
check(assert_type(idx.set_names("chinchilla"), "pd.Index[int]"), pd.Index, np.int64)
check(
assert_type(idx.set_names(["chinchilla"]), "pd.Index[int]"), pd.Index, np.int64
)

mi = pd.MultiIndex.from_arrays([[1, 2, 3], [4, 5, 6]], names=["elk", "owl"])
mi.set_names(["beluga", "pig"])
mi.set_names({"elk": "beluga", "owl": "pig"})