Skip to content

Commit c4331cf

Browse files
committed
Type Index methods: is_, ravel, take, repeat, to_flat_index,
`to_frame`, `set_names`, `rename`, remove undocumented `__copy__`, `__deepcopy__`, `__array_wrap__`
1 parent 9436296 commit c4331cf

File tree

2 files changed

+32
-15
lines changed

2 files changed

+32
-15
lines changed

pandas-stubs/core/indexes/base.pyi

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ from collections.abc import (
33
Callable,
44
Hashable,
55
Iterable,
6-
MutableMapping,
6+
Mapping,
77
Sequence,
88
)
99
from datetime import (
@@ -104,6 +104,7 @@ from pandas._typing import (
104104
SequenceNotStr,
105105
SliceType,
106106
SupportsDType,
107+
TakeIndexer,
107108
TimedeltaDtypeArg,
108109
TimestampDtypeArg,
109110
np_1darray,
@@ -350,40 +351,37 @@ class Index(IndexOpsMixin[S1], ElementOpsMixin[S1]):
350351
Index,
351352
]: ...
352353
@final
353-
def is_(self, other) -> bool: ...
354+
def is_(self, other: object) -> bool: ...
354355
def __len__(self) -> int: ...
355356
def __array__(
356357
self, dtype: _str | np.dtype = ..., copy: bool | None = ...
357358
) -> np_1darray: ...
358-
def __array_wrap__(self, result, context=...): ...
359359
@property
360360
def dtype(self) -> DtypeObj: ...
361361
@final
362-
def ravel(self, order: _str = ...): ...
362+
def ravel(self, order: _str = "C") -> Self: ...
363363
def view(self, cls=...): ...
364364
def astype(self, dtype: DtypeArg, copy: bool = True) -> Index: ...
365365
def take(
366366
self,
367-
indices,
367+
indices: TakeIndexer,
368368
axis: int = 0,
369369
allow_fill: bool = True,
370370
fill_value: Scalar | None = None,
371371
**kwargs: Any,
372+
) -> Self: ...
373+
def repeat(
374+
self, repeats: int | AnyArrayLikeInt | Sequence[int], axis: None = None
372375
): ...
373-
def repeat(self, repeats, axis=...): ...
374376
def copy(self, name: Hashable = ..., deep: bool = False) -> Self: ...
375-
@final
376-
def __copy__(self, **kwargs: Any): ...
377-
@final
378-
def __deepcopy__(self, memo: MutableMapping[int, Any] | None = None) -> Self: ...
379377
def format(
380378
self, name: bool = ..., formatter: Callable | None = ..., na_rep: _str = ...
381379
) -> list[_str]: ...
382-
def to_flat_index(self): ...
380+
def to_flat_index(self) -> Index: ...
383381
def to_series(
384382
self, index: Index | None = None, name: Hashable | None = None
385383
) -> Series[S1]: ...
386-
def to_frame(self, index: bool = True, name=...) -> DataFrame: ...
384+
def to_frame(self, index: bool = True, name: Hashable = ...) -> DataFrame: ...
387385
@property
388386
def name(self) -> Hashable | None: ...
389387
@name.setter
@@ -392,11 +390,17 @@ class Index(IndexOpsMixin[S1], ElementOpsMixin[S1]):
392390
def names(self) -> list[Hashable | None]: ...
393391
@names.setter
394392
def names(self, names: SequenceNotStr[Hashable | None]) -> None: ...
395-
def set_names(self, names, *, level=..., inplace: bool = ...): ...
393+
def set_names(
394+
self,
395+
names: Hashable | Sequence[Hashable] | Mapping[Hashable, Hashable],
396+
*,
397+
level: Level | Sequence[Level] | None = None,
398+
inplace: bool = False,
399+
) -> Self: ...
396400
@overload
397-
def rename(self, name, *, inplace: Literal[False] = False) -> Self: ...
401+
def rename(self, name: Hashable, *, inplace: Literal[False] = False) -> Self: ...
398402
@overload
399-
def rename(self, name, *, inplace: Literal[True]) -> None: ...
403+
def rename(self, name: Hashable, *, inplace: Literal[True]) -> None: ...
400404
@property
401405
def nlevels(self) -> int: ...
402406
def get_level_values(self, level: int | _str) -> Index: ...

tests/indexes/test_indexes.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1523,3 +1523,16 @@ def test_datetimeindex_where() -> None:
15231523

15241524
val_range = pd.RangeIndex(2).where(pd.Series([True, False]), 3)
15251525
check(assert_type(val_range, pd.Index), pd.RangeIndex)
1526+
1527+
1528+
def test_index_set_names() -> None:
1529+
"""Test Index.where with multiple types of other GH1419."""
1530+
idx = pd.Index([1, 2])
1531+
check(assert_type(idx.set_names("chinchilla"), "pd.Index[int]"), pd.Index, np.int64)
1532+
check(
1533+
assert_type(idx.set_names(["chinchilla"]), "pd.Index[int]"), pd.Index, np.int64
1534+
)
1535+
1536+
mi = pd.MultiIndex.from_arrays([[1, 2, 3], [4, 5, 6]], names=["elk", "owl"])
1537+
mi.set_names(["beluga", "pig"])
1538+
mi.set_names({"elk": "beluga", "owl": "pig"})

0 commit comments

Comments
 (0)