Skip to content

Commit 96f67d9

Browse files
committed
only allow for Mapping names in set_names for MultiIndex
1 parent 4934542 commit 96f67d9

File tree

3 files changed

+15
-6
lines changed

3 files changed

+15
-6
lines changed

pandas-stubs/core/indexes/base.pyi

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ from collections.abc import (
33
Callable,
44
Hashable,
55
Iterable,
6-
Mapping,
76
Sequence,
87
)
98
from datetime import (
@@ -82,6 +81,7 @@ from pandas._typing import (
8281
AnyArrayLikeInt,
8382
ArrayLike,
8483
AxesData,
84+
Axis,
8585
CategoryDtypeArg,
8686
DropKeep,
8787
Dtype,
@@ -365,19 +365,18 @@ class Index(IndexOpsMixin[S1], ElementOpsMixin[S1]):
365365
def take(
366366
self,
367367
indices: TakeIndexer,
368-
axis: int = 0,
368+
axis: Axis = 0,
369369
allow_fill: bool = True,
370370
fill_value: Scalar | None = None,
371371
**kwargs: Any,
372372
) -> Self: ...
373373
def repeat(
374374
self, repeats: int | AnyArrayLikeInt | Sequence[int], axis: None = None
375-
): ...
375+
) -> Self: ...
376376
def copy(self, name: Hashable = ..., deep: bool = False) -> Self: ...
377377
def format(
378378
self, name: bool = ..., formatter: Callable | None = ..., na_rep: _str = ...
379379
) -> list[_str]: ...
380-
def to_flat_index(self) -> Index: ...
381380
def to_series(
382381
self, index: Index | None = None, name: Hashable | None = None
383382
) -> Series[S1]: ...
@@ -392,7 +391,7 @@ class Index(IndexOpsMixin[S1], ElementOpsMixin[S1]):
392391
def names(self, names: SequenceNotStr[Hashable | None]) -> None: ...
393392
def set_names(
394393
self,
395-
names: Hashable | Sequence[Hashable] | Mapping[Any, Hashable],
394+
names: Hashable | Sequence[Hashable],
396395
*,
397396
level: Level | Sequence[Level] | None = None,
398397
inplace: bool = False,

pandas-stubs/core/indexes/multi.pyi

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@ from collections.abc import (
22
Callable,
33
Hashable,
44
Iterable,
5+
Mapping,
56
Sequence,
67
)
78
from typing import (
9+
Any,
810
final,
911
overload,
1012
)
@@ -115,7 +117,7 @@ class MultiIndex(Index):
115117
name: list[HashableT] = ...,
116118
allow_duplicates: bool = False,
117119
) -> pd.DataFrame: ...
118-
def to_flat_index(self): ...
120+
def to_flat_index(self) -> Index: ...
119121
def remove_unused_levels(self): ...
120122
@property
121123
def nlevels(self) -> int: ...
@@ -163,3 +165,10 @@ class MultiIndex(Index):
163165
def insert(self, loc, item): ...
164166
def delete(self, loc): ...
165167
def isin(self, values, level=...) -> np_1darray_bool: ...
168+
def set_names(
169+
self,
170+
names: Hashable | Sequence[Hashable] | Mapping[Any, Hashable],
171+
*,
172+
level: Level | Sequence[Level] | None = None,
173+
inplace: bool = False,
174+
) -> Self: ...

tests/indexes/test_indexes.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1529,6 +1529,7 @@ def test_index_set_names() -> None:
15291529
"""Test Index.where with multiple types of other GH1419."""
15301530
idx = pd.Index([1, 2])
15311531
check(assert_type(idx.set_names("chinchilla"), "pd.Index[int]"), pd.Index, np.int64)
1532+
check(assert_type(idx.set_names((0,)), "pd.Index[int]"), pd.Index, np.int64)
15321533
check(
15331534
assert_type(idx.set_names(["chinchilla"]), "pd.Index[int]"), pd.Index, np.int64
15341535
)

0 commit comments

Comments
 (0)