Skip to content

Commit e284165

Browse files
authored
Add typing for dropna for Index and MultiIndex (#232)
1 parent 713008d commit e284165

File tree

5 files changed

+20
-5
lines changed

5 files changed

+20
-5
lines changed

pandas-stubs/core/indexes/base.pyi

+1-1
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ class Index(IndexOpsMixin, PandasObject):
128128
def notna(self): ...
129129
notnull = ...
130130
def fillna(self, value=..., downcast=...): ...
131-
def dropna(self, how: _str = ...): ...
131+
def dropna(self, how: Literal["any", "all"] = ...) -> Index: ...
132132
def unique(self, level=...) -> Index: ...
133133
def drop_duplicates(
134134
self, keep: NaPosition | Literal[False] = ...

pandas-stubs/core/indexes/extension.pyi

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1+
from typing import Literal
2+
13
from pandas.core.indexes.base import Index
24

35
class ExtensionIndex(Index):
46
def __getitem__(self, key): ...
57
def __iter__(self): ...
6-
def dropna(self, how: str = ...): ...
8+
def dropna(self, how: Literal["any", "all"] = ...): ...
79
def repeat(self, repeats, axis=...): ...
810
def take(
911
self, indices, axis: int = ..., allow_fill: bool = ..., fill_value=..., **kwargs

pandas-stubs/core/indexes/multi.pyi

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from typing import (
22
Callable,
33
Hashable,
4+
Literal,
45
Sequence,
56
)
67

@@ -83,7 +84,7 @@ class MultiIndex(Index):
8384
def is_monotonic_decreasing(self) -> bool: ...
8485
def duplicated(self, keep: str = ...): ...
8586
def fillna(self, value=..., downcast=...) -> None: ...
86-
def dropna(self, how: str = ...): ...
87+
def dropna(self, how: Literal["any", "all"] = ...) -> MultiIndex: ...
8788
def get_value(self, series, key): ...
8889
def get_level_values(self, level: str | int) -> Index: ...
8990
def unique(self, level=...): ...

pandas-stubs/core/series.pyi

+2-2
Original file line numberDiff line numberDiff line change
@@ -869,7 +869,7 @@ class Series(IndexOpsMixin, NDFrame, Generic[S1]):
869869
def dropna(
870870
self,
871871
axis: SeriesAxisType = ...,
872-
how: _str | None = ...,
872+
how: Literal["any", "all"] | None = ...,
873873
*,
874874
inplace: Literal[True],
875875
) -> None: ...
@@ -878,7 +878,7 @@ class Series(IndexOpsMixin, NDFrame, Generic[S1]):
878878
self,
879879
axis: SeriesAxisType = ...,
880880
inplace: _bool = ...,
881-
how: _str | None = ...,
881+
how: Literal["any", "all"] | None = ...,
882882
) -> Series[S1]: ...
883883
def to_timestamp(
884884
self,

tests/test_indexes.py

+12
Original file line numberDiff line numberDiff line change
@@ -75,3 +75,15 @@ def test_str_split() -> None:
7575
ind = pd.Index(["a-b", "c-d"])
7676
check(assert_type(ind.str.split("-"), pd.Index), pd.Index)
7777
check(assert_type(ind.str.split("-", expand=True), pd.MultiIndex), pd.MultiIndex)
78+
79+
80+
def test_index_dropna():
81+
idx = pd.Index([1, 2])
82+
83+
check(assert_type(idx.dropna(how="all"), pd.Index), pd.Index)
84+
check(assert_type(idx.dropna(how="any"), pd.Index), pd.Index)
85+
86+
midx = pd.MultiIndex.from_arrays([[1, 2], [3, 4]])
87+
88+
check(assert_type(midx.dropna(how="all"), pd.MultiIndex), pd.MultiIndex)
89+
check(assert_type(midx.dropna(how="any"), pd.MultiIndex), pd.MultiIndex)

0 commit comments

Comments
 (0)