Skip to content

Commit 50bed6e

Browse files
Type Index methods: is_, ravel, take, repeat, to_flat_index, to_frame, set_names, rename, remove undocumented __copy__, __deepcopy__, __array_wrap__ (#1486)
* Type Index methods: `is_`, `ravel`, `take`, `repeat`, `to_flat_index`, `to_frame`, `set_names`, `rename`, remove undocumented `__copy__`, `__deepcopy__`, `__array_wrap__` * fix typing * only allow for `Mapping` `names` in `set_names` for `MultiIndex` * Apply suggestions from code review Co-authored-by: Yi-Fan Wang <[email protected]> * lint * Update pandas-stubs/core/indexes/base.pyi Co-authored-by: Yi-Fan Wang <[email protected]> * fix no_default usage * fix the extra `pd.Index([(1,)])` case --------- Co-authored-by: Yi-Fan Wang <[email protected]>
1 parent 5a47589 commit 50bed6e

File tree

3 files changed

+54
-19
lines changed

3 files changed

+54
-19
lines changed

pandas-stubs/core/indexes/base.pyi

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ from collections.abc import (
33
Callable,
44
Hashable,
55
Iterable,
6-
MutableMapping,
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,
@@ -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,36 @@ 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,
368-
axis: int = 0,
367+
indices: TakeIndexer,
368+
axis: Axis = 0,
369369
allow_fill: bool = True,
370370
fill_value: Scalar | None = None,
371371
**kwargs: Any,
372-
): ...
373-
def repeat(self, repeats, axis=...): ...
372+
) -> Self: ...
373+
def repeat(
374+
self, repeats: int | AnyArrayLikeInt | Sequence[int], axis: None = None
375+
) -> Self: ...
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): ...
383380
def to_series(
384381
self, index: Index | None = None, name: Hashable | None = None
385382
) -> Series[S1]: ...
386-
def to_frame(self, index: bool = True, name=...) -> DataFrame: ...
383+
def to_frame(self, index: bool = True, name: Hashable = ...) -> DataFrame: ...
387384
@property
388385
def name(self) -> Hashable | None: ...
389386
@name.setter
@@ -392,11 +389,17 @@ class Index(IndexOpsMixin[S1], ElementOpsMixin[S1]):
392389
def names(self) -> list[Hashable | None]: ...
393390
@names.setter
394391
def names(self, names: SequenceNotStr[Hashable | None]) -> None: ...
395-
def set_names(self, names, *, level=..., inplace: bool = ...): ...
392+
def set_names(
393+
self,
394+
names: Hashable | Sequence[Hashable],
395+
*,
396+
level: Level | Sequence[Level] | None = None,
397+
inplace: bool = False,
398+
) -> Self: ...
396399
@overload
397-
def rename(self, name, *, inplace: Literal[False] = False) -> Self: ...
400+
def rename(self, name: Hashable, *, inplace: Literal[False] = False) -> Self: ...
398401
@overload
399-
def rename(self, name, *, inplace: Literal[True]) -> None: ...
402+
def rename(self, name: Hashable, *, inplace: Literal[True]) -> None: ...
400403
@property
401404
def nlevels(self) -> int: ...
402405
def get_level_values(self, level: int | _str) -> Index: ...

pandas-stubs/core/indexes/multi.pyi

Lines changed: 11 additions & 2 deletions
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
)
@@ -109,13 +111,13 @@ class MultiIndex(Index):
109111
def dropna(self, how: AnyAll = "any") -> Self: ...
110112
def get_level_values(self, level: str | int) -> Index: ...
111113
def unique(self, level=...): ...
112-
def to_frame(
114+
def to_frame( # type: ignore[override] # pyright: ignore[reportIncompatibleMethodOverride]
113115
self,
114116
index: bool = True,
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: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1523,3 +1523,26 @@ 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(
1532+
assert_type(idx.set_names("chinchilla"), "pd.Index[int]"), pd.Index, np.integer
1533+
)
1534+
check(assert_type(idx.set_names((0,)), "pd.Index[int]"), pd.Index, np.integer)
1535+
check(
1536+
assert_type(idx.set_names(["chinchilla"]), "pd.Index[int]"),
1537+
pd.Index,
1538+
np.integer,
1539+
)
1540+
1541+
mi = pd.MultiIndex.from_arrays([[1, 2, 3], [4, 5, 6]], names=["elk", "owl"])
1542+
check(assert_type(mi.set_names(["beluga", "pig"]), pd.MultiIndex), pd.MultiIndex)
1543+
check(
1544+
assert_type(mi.set_names({"elk": "beluga", "owl": "pig"}), pd.MultiIndex),
1545+
pd.MultiIndex,
1546+
)
1547+
mi = cast("pd.MultiIndex", pd.Index([(1,)]))
1548+
check(assert_type(mi.set_names(1), pd.MultiIndex), pd.MultiIndex, tuple)

0 commit comments

Comments
 (0)