diff --git a/pandas-stubs/core/indexes/base.pyi b/pandas-stubs/core/indexes/base.pyi index e29561eb3..584548d97 100644 --- a/pandas-stubs/core/indexes/base.pyi +++ b/pandas-stubs/core/indexes/base.pyi @@ -351,7 +351,7 @@ class Index(IndexOpsMixin[S1], ElementOpsMixin[S1]): Index, ]: ... @final - def is_(self, other: object) -> bool: ... + def is_(self, other: Any) -> bool: ... def __len__(self) -> int: ... def __array__( self, dtype: _str | np.dtype = ..., copy: bool | None = ... @@ -403,7 +403,7 @@ class Index(IndexOpsMixin[S1], ElementOpsMixin[S1]): @property def nlevels(self) -> int: ... def get_level_values(self, level: int | _str) -> Index: ... - def droplevel(self, level: Level | list[Level] = 0): ... + def droplevel(self, level: Level | Sequence[Level] = 0) -> Self: ... @property def is_monotonic_increasing(self) -> bool: ... @property @@ -414,16 +414,13 @@ class Index(IndexOpsMixin[S1], ElementOpsMixin[S1]): def has_duplicates(self) -> bool: ... @property def inferred_type(self) -> _str: ... - def __reduce__(self): ... @property def hasnans(self) -> bool: ... @final - def isna(self): ... - isnull = ... + def isna(self) -> Index[bool]: ... @final - def notna(self): ... - notnull = ... - def fillna(self, value=...): ... + def notna(self) -> Index[bool]: ... + def fillna(self, value: Scalar) -> Index: ... def dropna(self, how: AnyAll = "any") -> Self: ... def unique(self, level: Hashable | None = None) -> Self: ... def drop_duplicates(self, *, keep: DropKeep = ...) -> Self: ... @@ -489,7 +486,7 @@ class Index(IndexOpsMixin[S1], ElementOpsMixin[S1]): ) -> Index: ... @property def values(self) -> np_1darray: ... - def memory_usage(self, deep: bool = False): ... + def memory_usage(self, deep: bool = False) -> int: ... @overload def where( self, @@ -503,8 +500,6 @@ class Index(IndexOpsMixin[S1], ElementOpsMixin[S1]): other: Scalar | AnyArrayLike | None = None, ) -> Index: ... def __contains__(self, key: Hashable) -> bool: ... - @final - def __setitem__(self, key, value) -> None: ... @overload def __getitem__( self, @@ -519,9 +514,9 @@ class Index(IndexOpsMixin[S1], ElementOpsMixin[S1]): @overload def append(self, other: Index | Sequence[Index]) -> Index: ... def putmask(self, mask, value): ... - def equals(self, other: object) -> bool: ... + def equals(self, other: Any) -> bool: ... @final - def identical(self, other) -> bool: ... + def identical(self, other: Any) -> bool: ... @final def asof(self, label): ... def asof_locs(self, where, mask): ... diff --git a/pandas-stubs/core/indexes/multi.pyi b/pandas-stubs/core/indexes/multi.pyi index 544ff061a..3b3564295 100644 --- a/pandas-stubs/core/indexes/multi.pyi +++ b/pandas-stubs/core/indexes/multi.pyi @@ -109,6 +109,7 @@ class MultiIndex(Index): def is_monotonic_decreasing(self) -> bool: ... def duplicated(self, keep: DropKeep = "first"): ... def dropna(self, how: AnyAll = "any") -> Self: ... + def droplevel(self, level: Level | Sequence[Level] = 0) -> MultiIndex | Index: ... # type: ignore[override] def get_level_values(self, level: str | int) -> Index: ... def unique(self, level=...): ... def to_frame( # type: ignore[override] # pyright: ignore[reportIncompatibleMethodOverride] diff --git a/tests/indexes/test_indexes.py b/tests/indexes/test_indexes.py index 45cd97fc1..5b3d9416a 100644 --- a/tests/indexes/test_indexes.py +++ b/tests/indexes/test_indexes.py @@ -1526,7 +1526,6 @@ def test_datetimeindex_where() -> None: 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.integer @@ -1546,3 +1545,21 @@ def test_index_set_names() -> None: ) mi = cast("pd.MultiIndex", pd.Index([(1,)])) check(assert_type(mi.set_names(1), pd.MultiIndex), pd.MultiIndex, tuple) + + +def test_index_droplevel() -> None: + idx = pd.Index([1, 2]) + check(assert_type(idx.droplevel([]), "pd.Index[int]"), pd.Index, np.integer) + mi = pd.MultiIndex.from_arrays([[1, 2, 3], [4, 5, 6]], names=["elk", "owl"]) + check(assert_type(mi.droplevel([]), pd.MultiIndex | pd.Index), pd.MultiIndex) + check(assert_type(mi.droplevel([0]), pd.MultiIndex | pd.Index), pd.Index) + check(assert_type(mi.droplevel((0,)), pd.MultiIndex | pd.Index), pd.Index) + check(assert_type(mi.droplevel(["elk"]), pd.MultiIndex | pd.Index), pd.Index) + check(assert_type(mi.droplevel(("elk",)), pd.MultiIndex | pd.Index), pd.Index) + check(assert_type(mi.droplevel(0), pd.MultiIndex | pd.Index), pd.Index) + + +def test_index_setitem() -> None: + idx = pd.Index([1, 2]) + if TYPE_CHECKING_INVALID_USAGE: + idx[0] = 999 # type: ignore[index] # pyright: ignore[reportIndexIssue]