Skip to content

Commit 2cb628d

Browse files
authored
TYP: pd.isna (#46222)
1 parent aa3e420 commit 2cb628d

File tree

5 files changed

+100
-19
lines changed

5 files changed

+100
-19
lines changed

pandas/core/arrays/boolean.py

+9-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
from __future__ import annotations
22

33
import numbers
4-
from typing import TYPE_CHECKING
4+
from typing import (
5+
TYPE_CHECKING,
6+
cast,
7+
)
58

69
import numpy as np
710

@@ -31,6 +34,8 @@
3134
if TYPE_CHECKING:
3235
import pyarrow
3336

37+
from pandas._typing import npt
38+
3439

3540
@register_extension_dtype
3641
class BooleanDtype(BaseMaskedDtype):
@@ -188,7 +193,9 @@ def coerce_to_array(
188193
if inferred_dtype not in ("boolean", "empty") + integer_like:
189194
raise TypeError("Need to pass bool-like values")
190195

191-
mask_values = isna(values_object)
196+
# mypy does not narrow the type of mask_values to npt.NDArray[np.bool_]
197+
# within this branch, it assumes it can also be None
198+
mask_values = cast("npt.NDArray[np.bool_]", isna(values_object))
192199
values = np.zeros(len(values), dtype=bool)
193200
values[~mask_values] = values_object[~mask_values].astype(bool)
194201

pandas/core/base.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -769,7 +769,9 @@ def hasnans(self) -> bool:
769769
770770
Enables various performance speedups.
771771
"""
772-
return bool(isna(self).any())
772+
# error: Item "bool" of "Union[bool, ndarray[Any, dtype[bool_]], NDFrame]"
773+
# has no attribute "any"
774+
return bool(isna(self).any()) # type: ignore[union-attr]
773775

774776
def isna(self):
775777
return isna(self._values)

pandas/core/dtypes/missing.py

+76-8
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55

66
from decimal import Decimal
77
from functools import partial
8+
from typing import (
9+
TYPE_CHECKING,
10+
overload,
11+
)
812

913
import numpy as np
1014

@@ -16,11 +20,6 @@
1620
NaT,
1721
iNaT,
1822
)
19-
from pandas._typing import (
20-
ArrayLike,
21-
DtypeObj,
22-
npt,
23-
)
2423

2524
from pandas.core.dtypes.common import (
2625
DT64NS_DTYPE,
@@ -54,6 +53,19 @@
5453
)
5554
from pandas.core.dtypes.inference import is_list_like
5655

56+
if TYPE_CHECKING:
57+
from pandas._typing import (
58+
ArrayLike,
59+
DtypeObj,
60+
NDFrame,
61+
NDFrameT,
62+
Scalar,
63+
npt,
64+
)
65+
66+
from pandas.core.indexes.base import Index
67+
68+
5769
isposinf_scalar = libmissing.isposinf_scalar
5870
isneginf_scalar = libmissing.isneginf_scalar
5971

@@ -63,7 +75,35 @@
6375
_dtype_str = np.dtype(str)
6476

6577

66-
def isna(obj):
78+
@overload
79+
def isna(obj: Scalar) -> bool:
80+
...
81+
82+
83+
@overload
84+
def isna(
85+
obj: ArrayLike | Index | list,
86+
) -> npt.NDArray[np.bool_]:
87+
...
88+
89+
90+
@overload
91+
def isna(obj: NDFrameT) -> NDFrameT:
92+
...
93+
94+
95+
# handle unions
96+
@overload
97+
def isna(obj: NDFrameT | ArrayLike | Index | list) -> NDFrameT | npt.NDArray[np.bool_]:
98+
...
99+
100+
101+
@overload
102+
def isna(obj: object) -> bool | npt.NDArray[np.bool_] | NDFrame:
103+
...
104+
105+
106+
def isna(obj: object) -> bool | npt.NDArray[np.bool_] | NDFrame:
67107
"""
68108
Detect missing values for an array-like object.
69109
@@ -284,7 +324,35 @@ def _isna_string_dtype(values: np.ndarray, inf_as_na: bool) -> npt.NDArray[np.bo
284324
return result
285325

286326

287-
def notna(obj):
327+
@overload
328+
def notna(obj: Scalar) -> bool:
329+
...
330+
331+
332+
@overload
333+
def notna(
334+
obj: ArrayLike | Index | list,
335+
) -> npt.NDArray[np.bool_]:
336+
...
337+
338+
339+
@overload
340+
def notna(obj: NDFrameT) -> NDFrameT:
341+
...
342+
343+
344+
# handle unions
345+
@overload
346+
def notna(obj: NDFrameT | ArrayLike | Index | list) -> NDFrameT | npt.NDArray[np.bool_]:
347+
...
348+
349+
350+
@overload
351+
def notna(obj: object) -> bool | npt.NDArray[np.bool_] | NDFrame:
352+
...
353+
354+
355+
def notna(obj: object) -> bool | npt.NDArray[np.bool_] | NDFrame:
288356
"""
289357
Detect non-missing values for an array-like object.
290358
@@ -362,7 +430,7 @@ def notna(obj):
362430
Name: 1, dtype: bool
363431
"""
364432
res = isna(obj)
365-
if is_scalar(res):
433+
if isinstance(res, bool):
366434
return not res
367435
return ~res
368436

pandas/core/window/ewm.py

+7-5
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33
import datetime
44
from functools import partial
55
from textwrap import dedent
6-
from typing import TYPE_CHECKING
6+
from typing import (
7+
TYPE_CHECKING,
8+
cast,
9+
)
710
import warnings
811

912
import numpy as np
@@ -380,12 +383,11 @@ def __init__(
380383
FutureWarning,
381384
stacklevel=find_stack_level(),
382385
)
383-
self.times = self._selected_obj[self.times]
386+
# self.times cannot be str anymore
387+
self.times = cast("Series", self._selected_obj[self.times])
384388
if not is_datetime64_ns_dtype(self.times):
385389
raise ValueError("times must be datetime64[ns] dtype.")
386-
# error: Argument 1 to "len" has incompatible type "Union[str, ndarray,
387-
# NDFrameT, None]"; expected "Sized"
388-
if len(self.times) != len(obj): # type: ignore[arg-type]
390+
if len(self.times) != len(obj):
389391
raise ValueError("times must be the same length as the object.")
390392
if not isinstance(self.halflife, (str, datetime.timedelta)):
391393
raise ValueError(

pandas/io/parsers/base_parser.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -855,9 +855,11 @@ def _check_data_length(
855855
data: list of array-likes containing the data column-wise.
856856
"""
857857
if not self.index_col and len(columns) != len(data) and columns:
858-
if len(columns) == len(data) - 1 and np.all(
859-
(is_object_dtype(data[-1]) and data[-1] == "") | isna(data[-1])
860-
):
858+
empty_str = is_object_dtype(data[-1]) and data[-1] == ""
859+
# error: No overload variant of "__ror__" of "ndarray" matches
860+
# argument type "ExtensionArray"
861+
empty_str_or_na = empty_str | isna(data[-1]) # type: ignore[operator]
862+
if len(columns) == len(data) - 1 and np.all(empty_str_or_na):
861863
return
862864
warnings.warn(
863865
"Length of header or names does not match length of data. This leads "

0 commit comments

Comments
 (0)