Skip to content

Commit a4bfa19

Browse files
authored
Type Index methods: droplevel, __reduce__, isna, isnull, notna, notnull, fillna, memory_usage, __setitem__, equals, identical (#1500)
* Type Index methods: `droplevel`, `__reduce__`, `isna`, `isnull`, `notna`, `notnull`, `fillna`, `memory_usage`, `__setitem__`, `equals`, `identical` * add some droplevel tests * appease mypy * remove undocumented isnull / fillnull * test invalid setitem
1 parent ab8e0a5 commit a4bfa19

File tree

3 files changed

+27
-14
lines changed

3 files changed

+27
-14
lines changed

pandas-stubs/core/indexes/base.pyi

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ class Index(IndexOpsMixin[S1], ElementOpsMixin[S1]):
351351
Index,
352352
]: ...
353353
@final
354-
def is_(self, other: object) -> bool: ...
354+
def is_(self, other: Any) -> bool: ...
355355
def __len__(self) -> int: ...
356356
def __array__(
357357
self, dtype: _str | np.dtype = ..., copy: bool | None = ...
@@ -403,7 +403,7 @@ class Index(IndexOpsMixin[S1], ElementOpsMixin[S1]):
403403
@property
404404
def nlevels(self) -> int: ...
405405
def get_level_values(self, level: int | _str) -> Index: ...
406-
def droplevel(self, level: Level | list[Level] = 0): ...
406+
def droplevel(self, level: Level | Sequence[Level] = 0) -> Self: ...
407407
@property
408408
def is_monotonic_increasing(self) -> bool: ...
409409
@property
@@ -414,16 +414,13 @@ class Index(IndexOpsMixin[S1], ElementOpsMixin[S1]):
414414
def has_duplicates(self) -> bool: ...
415415
@property
416416
def inferred_type(self) -> _str: ...
417-
def __reduce__(self): ...
418417
@property
419418
def hasnans(self) -> bool: ...
420419
@final
421-
def isna(self): ...
422-
isnull = ...
420+
def isna(self) -> Index[bool]: ...
423421
@final
424-
def notna(self): ...
425-
notnull = ...
426-
def fillna(self, value=...): ...
422+
def notna(self) -> Index[bool]: ...
423+
def fillna(self, value: Scalar) -> Index: ...
427424
def dropna(self, how: AnyAll = "any") -> Self: ...
428425
def unique(self, level: Hashable | None = None) -> Self: ...
429426
def drop_duplicates(self, *, keep: DropKeep = ...) -> Self: ...
@@ -489,7 +486,7 @@ class Index(IndexOpsMixin[S1], ElementOpsMixin[S1]):
489486
) -> Index: ...
490487
@property
491488
def values(self) -> np_1darray: ...
492-
def memory_usage(self, deep: bool = False): ...
489+
def memory_usage(self, deep: bool = False) -> int: ...
493490
@overload
494491
def where(
495492
self,
@@ -503,8 +500,6 @@ class Index(IndexOpsMixin[S1], ElementOpsMixin[S1]):
503500
other: Scalar | AnyArrayLike | None = None,
504501
) -> Index: ...
505502
def __contains__(self, key: Hashable) -> bool: ...
506-
@final
507-
def __setitem__(self, key, value) -> None: ...
508503
@overload
509504
def __getitem__(
510505
self,
@@ -519,9 +514,9 @@ class Index(IndexOpsMixin[S1], ElementOpsMixin[S1]):
519514
@overload
520515
def append(self, other: Index | Sequence[Index]) -> Index: ...
521516
def putmask(self, mask, value): ...
522-
def equals(self, other: object) -> bool: ...
517+
def equals(self, other: Any) -> bool: ...
523518
@final
524-
def identical(self, other) -> bool: ...
519+
def identical(self, other: Any) -> bool: ...
525520
@final
526521
def asof(self, label): ...
527522
def asof_locs(self, where, mask): ...

pandas-stubs/core/indexes/multi.pyi

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ class MultiIndex(Index):
109109
def is_monotonic_decreasing(self) -> bool: ...
110110
def duplicated(self, keep: DropKeep = "first"): ...
111111
def dropna(self, how: AnyAll = "any") -> Self: ...
112+
def droplevel(self, level: Level | Sequence[Level] = 0) -> MultiIndex | Index: ... # type: ignore[override]
112113
def get_level_values(self, level: str | int) -> Index: ...
113114
def unique(self, level=...): ...
114115
def to_frame( # type: ignore[override] # pyright: ignore[reportIncompatibleMethodOverride]

tests/indexes/test_indexes.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1526,7 +1526,6 @@ def test_datetimeindex_where() -> None:
15261526

15271527

15281528
def test_index_set_names() -> None:
1529-
"""Test Index.where with multiple types of other GH1419."""
15301529
idx = pd.Index([1, 2])
15311530
check(
15321531
assert_type(idx.set_names("chinchilla"), "pd.Index[int]"), pd.Index, np.integer
@@ -1546,3 +1545,21 @@ def test_index_set_names() -> None:
15461545
)
15471546
mi = cast("pd.MultiIndex", pd.Index([(1,)]))
15481547
check(assert_type(mi.set_names(1), pd.MultiIndex), pd.MultiIndex, tuple)
1548+
1549+
1550+
def test_index_droplevel() -> None:
1551+
idx = pd.Index([1, 2])
1552+
check(assert_type(idx.droplevel([]), "pd.Index[int]"), pd.Index, np.integer)
1553+
mi = pd.MultiIndex.from_arrays([[1, 2, 3], [4, 5, 6]], names=["elk", "owl"])
1554+
check(assert_type(mi.droplevel([]), pd.MultiIndex | pd.Index), pd.MultiIndex)
1555+
check(assert_type(mi.droplevel([0]), pd.MultiIndex | pd.Index), pd.Index)
1556+
check(assert_type(mi.droplevel((0,)), pd.MultiIndex | pd.Index), pd.Index)
1557+
check(assert_type(mi.droplevel(["elk"]), pd.MultiIndex | pd.Index), pd.Index)
1558+
check(assert_type(mi.droplevel(("elk",)), pd.MultiIndex | pd.Index), pd.Index)
1559+
check(assert_type(mi.droplevel(0), pd.MultiIndex | pd.Index), pd.Index)
1560+
1561+
1562+
def test_index_setitem() -> None:
1563+
idx = pd.Index([1, 2])
1564+
if TYPE_CHECKING_INVALID_USAGE:
1565+
idx[0] = 999 # type: ignore[index] # pyright: ignore[reportIndexIssue]

0 commit comments

Comments
 (0)