Skip to content

Commit 60c2940

Browse files
authored
TYP: assorted (#46121)
1 parent 93ba57a commit 60c2940

File tree

23 files changed

+98
-93
lines changed

23 files changed

+98
-93
lines changed

pandas/core/algorithms.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -534,7 +534,7 @@ def factorize_array(
534534
na_sentinel: int = -1,
535535
size_hint: int | None = None,
536536
na_value=None,
537-
mask: np.ndarray | None = None,
537+
mask: npt.NDArray[np.bool_] | None = None,
538538
) -> tuple[npt.NDArray[np.intp], np.ndarray]:
539539
"""
540540
Factorize a numpy array to codes and uniques.

pandas/core/array_algos/masked_reductions.py

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,26 @@
22
masked_reductions.py is for reduction algorithms using a mask-based approach
33
for missing values.
44
"""
5+
from __future__ import annotations
56

6-
from typing import (
7-
Callable,
8-
Optional,
9-
)
7+
from typing import Callable
108

119
import numpy as np
1210

1311
from pandas._libs import missing as libmissing
12+
from pandas._typing import npt
1413

1514
from pandas.core.nanops import check_below_min_count
1615

1716

1817
def _sumprod(
1918
func: Callable,
2019
values: np.ndarray,
21-
mask: np.ndarray,
20+
mask: npt.NDArray[np.bool_],
2221
*,
2322
skipna: bool = True,
2423
min_count: int = 0,
25-
axis: Optional[int] = None,
24+
axis: int | None = None,
2625
):
2726
"""
2827
Sum or product for 1D masked array.
@@ -33,7 +32,7 @@ def _sumprod(
3332
values : np.ndarray
3433
Numpy array with the values (can be of any dtype that support the
3534
operation).
36-
mask : np.ndarray
35+
mask : np.ndarray[bool]
3736
Boolean numpy array (True values indicate missing values).
3837
skipna : bool, default True
3938
Whether to skip NA.
@@ -58,11 +57,11 @@ def _sumprod(
5857

5958
def sum(
6059
values: np.ndarray,
61-
mask: np.ndarray,
60+
mask: npt.NDArray[np.bool_],
6261
*,
6362
skipna: bool = True,
6463
min_count: int = 0,
65-
axis: Optional[int] = None,
64+
axis: int | None = None,
6665
):
6766
return _sumprod(
6867
np.sum, values=values, mask=mask, skipna=skipna, min_count=min_count, axis=axis
@@ -71,11 +70,11 @@ def sum(
7170

7271
def prod(
7372
values: np.ndarray,
74-
mask: np.ndarray,
73+
mask: npt.NDArray[np.bool_],
7574
*,
7675
skipna: bool = True,
7776
min_count: int = 0,
78-
axis: Optional[int] = None,
77+
axis: int | None = None,
7978
):
8079
return _sumprod(
8180
np.prod, values=values, mask=mask, skipna=skipna, min_count=min_count, axis=axis
@@ -85,10 +84,10 @@ def prod(
8584
def _minmax(
8685
func: Callable,
8786
values: np.ndarray,
88-
mask: np.ndarray,
87+
mask: npt.NDArray[np.bool_],
8988
*,
9089
skipna: bool = True,
91-
axis: Optional[int] = None,
90+
axis: int | None = None,
9291
):
9392
"""
9493
Reduction for 1D masked array.
@@ -99,7 +98,7 @@ def _minmax(
9998
values : np.ndarray
10099
Numpy array with the values (can be of any dtype that support the
101100
operation).
102-
mask : np.ndarray
101+
mask : np.ndarray[bool]
103102
Boolean numpy array (True values indicate missing values).
104103
skipna : bool, default True
105104
Whether to skip NA.
@@ -122,26 +121,26 @@ def _minmax(
122121

123122
def min(
124123
values: np.ndarray,
125-
mask: np.ndarray,
124+
mask: npt.NDArray[np.bool_],
126125
*,
127126
skipna: bool = True,
128-
axis: Optional[int] = None,
127+
axis: int | None = None,
129128
):
130129
return _minmax(np.min, values=values, mask=mask, skipna=skipna, axis=axis)
131130

132131

133132
def max(
134133
values: np.ndarray,
135-
mask: np.ndarray,
134+
mask: npt.NDArray[np.bool_],
136135
*,
137136
skipna: bool = True,
138-
axis: Optional[int] = None,
137+
axis: int | None = None,
139138
):
140139
return _minmax(np.max, values=values, mask=mask, skipna=skipna, axis=axis)
141140

142141

143142
# TODO: axis kwarg
144-
def mean(values: np.ndarray, mask: np.ndarray, skipna: bool = True):
143+
def mean(values: np.ndarray, mask: npt.NDArray[np.bool_], skipna: bool = True):
145144
if not values.size or mask.all():
146145
return libmissing.NA
147146
_sum = _sumprod(np.sum, values=values, mask=mask, skipna=skipna)

pandas/core/array_algos/replace.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from pandas._typing import (
1616
ArrayLike,
1717
Scalar,
18+
npt,
1819
)
1920

2021
from pandas.core.dtypes.common import (
@@ -42,7 +43,7 @@ def should_use_regex(regex: bool, to_replace: Any) -> bool:
4243

4344

4445
def compare_or_regex_search(
45-
a: ArrayLike, b: Scalar | Pattern, regex: bool, mask: np.ndarray
46+
a: ArrayLike, b: Scalar | Pattern, regex: bool, mask: npt.NDArray[np.bool_]
4647
) -> ArrayLike | bool:
4748
"""
4849
Compare two array-like inputs of the same shape or two scalar values
@@ -116,7 +117,9 @@ def _check_comparison_types(
116117
return result
117118

118119

119-
def replace_regex(values: ArrayLike, rx: re.Pattern, value, mask: np.ndarray | None):
120+
def replace_regex(
121+
values: ArrayLike, rx: re.Pattern, value, mask: npt.NDArray[np.bool_] | None
122+
):
120123
"""
121124
Parameters
122125
----------

pandas/core/arrays/_mixins.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,7 @@ def _putmask(self, mask: npt.NDArray[np.bool_], value) -> None:
367367
np.putmask(self._ndarray, mask, value)
368368

369369
def _where(
370-
self: NDArrayBackedExtensionArrayT, mask: np.ndarray, value
370+
self: NDArrayBackedExtensionArrayT, mask: npt.NDArray[np.bool_], value
371371
) -> NDArrayBackedExtensionArrayT:
372372
"""
373373
Analogue to np.where(mask, self, value)

pandas/core/arrays/masked.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,13 +99,15 @@ class BaseMaskedArray(OpsMixin, ExtensionArray):
9999
_internal_fill_value: Scalar
100100
# our underlying data and mask are each ndarrays
101101
_data: np.ndarray
102-
_mask: np.ndarray
102+
_mask: npt.NDArray[np.bool_]
103103

104104
# Fill values used for any/all
105105
_truthy_value = Scalar # bool(_truthy_value) = True
106106
_falsey_value = Scalar # bool(_falsey_value) = False
107107

108-
def __init__(self, values: np.ndarray, mask: np.ndarray, copy: bool = False):
108+
def __init__(
109+
self, values: np.ndarray, mask: npt.NDArray[np.bool_], copy: bool = False
110+
):
109111
# values is supposed to already be validated in the subclass
110112
if not (isinstance(mask, np.ndarray) and mask.dtype == np.bool_):
111113
raise TypeError(

pandas/core/arrays/numeric.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from pandas._typing import (
1818
Dtype,
1919
DtypeObj,
20+
npt,
2021
)
2122
from pandas.errors import AbstractMethodError
2223
from pandas.util._decorators import cache_readonly
@@ -219,7 +220,9 @@ class NumericArray(BaseMaskedArray):
219220

220221
_dtype_cls: type[NumericDtype]
221222

222-
def __init__(self, values: np.ndarray, mask: np.ndarray, copy: bool = False):
223+
def __init__(
224+
self, values: np.ndarray, mask: npt.NDArray[np.bool_], copy: bool = False
225+
):
223226
checker = self._dtype_cls._checker
224227
if not (isinstance(values, np.ndarray) and checker(values.dtype)):
225228
descr = (

pandas/core/groupby/groupby.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3607,13 +3607,13 @@ def tail(self, n=5):
36073607
return self._mask_selected_obj(mask)
36083608

36093609
@final
3610-
def _mask_selected_obj(self, mask: np.ndarray) -> NDFrameT:
3610+
def _mask_selected_obj(self, mask: npt.NDArray[np.bool_]) -> NDFrameT:
36113611
"""
36123612
Return _selected_obj with mask applied to the correct axis.
36133613
36143614
Parameters
36153615
----------
3616-
mask : np.ndarray
3616+
mask : np.ndarray[bool]
36173617
Boolean mask to apply.
36183618
36193619
Returns

pandas/core/groupby/ops.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -437,8 +437,8 @@ def _cython_op_ndim_compat(
437437
min_count: int,
438438
ngroups: int,
439439
comp_ids: np.ndarray,
440-
mask: np.ndarray | None = None,
441-
result_mask: np.ndarray | None = None,
440+
mask: npt.NDArray[np.bool_] | None = None,
441+
result_mask: npt.NDArray[np.bool_] | None = None,
442442
**kwargs,
443443
) -> np.ndarray:
444444
if values.ndim == 1:
@@ -481,8 +481,8 @@ def _call_cython_op(
481481
min_count: int,
482482
ngroups: int,
483483
comp_ids: np.ndarray,
484-
mask: np.ndarray | None,
485-
result_mask: np.ndarray | None,
484+
mask: npt.NDArray[np.bool_] | None,
485+
result_mask: npt.NDArray[np.bool_] | None,
486486
**kwargs,
487487
) -> np.ndarray: # np.ndarray[ndim=2]
488488
orig_values = values

pandas/core/indexes/base.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5548,7 +5548,9 @@ def asof(self, label):
55485548

55495549
return self[loc]
55505550

5551-
def asof_locs(self, where: Index, mask: np.ndarray) -> npt.NDArray[np.intp]:
5551+
def asof_locs(
5552+
self, where: Index, mask: npt.NDArray[np.bool_]
5553+
) -> npt.NDArray[np.intp]:
55525554
"""
55535555
Return the locations (indices) of labels in the index.
55545556

pandas/core/indexes/period.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
from pandas._typing import (
2424
Dtype,
2525
DtypeObj,
26+
npt,
2627
)
2728
from pandas.util._decorators import doc
2829
from pandas.util._exceptions import find_stack_level
@@ -327,7 +328,7 @@ def _is_comparable_dtype(self, dtype: DtypeObj) -> bool:
327328
# ------------------------------------------------------------------------
328329
# Index Methods
329330

330-
def asof_locs(self, where: Index, mask: np.ndarray) -> np.ndarray:
331+
def asof_locs(self, where: Index, mask: npt.NDArray[np.bool_]) -> np.ndarray:
331332
"""
332333
where : array of timestamps
333334
mask : np.ndarray[bool]

0 commit comments

Comments
 (0)