Skip to content

Commit 28ca3ed

Browse files
jbrockmendelyehoshuadimarsky
authored andcommitted
TYP: assorted (pandas-dev#46121)
1 parent 6ddaad6 commit 28ca3ed

23 files changed

+98
-93
lines changed

pandas/core/algorithms.py

+1-1
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

+18-19
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

+5-2
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

+1-1
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

+4-2
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

+4-1
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

+2-2
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

+4-4
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

+3-1
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

+2-1
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]

pandas/core/internals/array_manager.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
from pandas._typing import (
2222
ArrayLike,
2323
DtypeObj,
24+
npt,
2425
)
2526
from pandas.util._validators import validate_bool_kwarg
2627

@@ -568,7 +569,7 @@ def reindex_indexer(
568569
def _reindex_indexer(
569570
self: T,
570571
new_axis,
571-
indexer,
572+
indexer: npt.NDArray[np.intp] | None,
572573
axis: int,
573574
fill_value=None,
574575
allow_dups: bool = False,
@@ -579,7 +580,7 @@ def _reindex_indexer(
579580
Parameters
580581
----------
581582
new_axis : Index
582-
indexer : ndarray of int64 or None
583+
indexer : ndarray[intp] or None
583584
axis : int
584585
fill_value : object, default None
585586
allow_dups : bool, default False

pandas/core/internals/blocks.py

+6-11
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,6 @@
8787
replace_regex,
8888
should_use_regex,
8989
)
90-
from pandas.core.array_algos.take import take_nd
9190
from pandas.core.array_algos.transforms import shift
9291
from pandas.core.arrays import (
9392
Categorical,
@@ -736,7 +735,7 @@ def _replace_coerce(
736735
self,
737736
to_replace,
738737
value,
739-
mask: np.ndarray,
738+
mask: npt.NDArray[np.bool_],
740739
inplace: bool = True,
741740
regex: bool = False,
742741
) -> list[Block]:
@@ -827,19 +826,14 @@ def set_inplace(self, locs, values: ArrayLike) -> None:
827826

828827
def take_nd(
829828
self,
830-
indexer,
829+
indexer: npt.NDArray[np.intp],
831830
axis: int,
832831
new_mgr_locs: BlockPlacement | None = None,
833832
fill_value=lib.no_default,
834833
) -> Block:
835834
"""
836-
Take values according to indexer and return them as a block.bb
837-
835+
Take values according to indexer and return them as a block.
838836
"""
839-
# algos.take_nd dispatches for DatetimeTZBlock, CategoricalBlock
840-
# so need to preserve types
841-
# sparse is treated like an ndarray, but needs .get_values() shaping
842-
843837
values = self.values
844838

845839
if fill_value is lib.no_default:
@@ -848,6 +842,7 @@ def take_nd(
848842
else:
849843
allow_fill = True
850844

845+
# Note: algos.take_nd has upcast logic similar to coerce_to_target_dtype
851846
new_values = algos.take_nd(
852847
values, indexer, axis=axis, allow_fill=allow_fill, fill_value=fill_value
853848
)
@@ -1727,7 +1722,7 @@ def is_numeric(self):
17271722

17281723
def take_nd(
17291724
self,
1730-
indexer,
1725+
indexer: npt.NDArray[np.intp],
17311726
axis: int = 0,
17321727
new_mgr_locs: BlockPlacement | None = None,
17331728
fill_value=lib.no_default,
@@ -2259,7 +2254,7 @@ def to_native_types(
22592254
"""convert to our native types format"""
22602255
if isinstance(values, Categorical):
22612256
# GH#40754 Convert categorical datetimes to datetime array
2262-
values = take_nd(
2257+
values = algos.take_nd(
22632258
values.categories._values,
22642259
ensure_platform_int(values._codes),
22652260
fill_value=na_rep,

pandas/core/internals/managers.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -597,7 +597,7 @@ def consolidate(self: T) -> T:
597597
def reindex_indexer(
598598
self: T,
599599
new_axis: Index,
600-
indexer,
600+
indexer: npt.NDArray[np.intp] | None,
601601
axis: int,
602602
fill_value=None,
603603
allow_dups: bool = False,
@@ -610,7 +610,7 @@ def reindex_indexer(
610610
Parameters
611611
----------
612612
new_axis : Index
613-
indexer : ndarray of int64 or None
613+
indexer : ndarray[intp] or None
614614
axis : int
615615
fill_value : object, default None
616616
allow_dups : bool, default False

pandas/core/missing.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
from pandas import Index
4444

4545

46-
def check_value_size(value, mask: np.ndarray, length: int):
46+
def check_value_size(value, mask: npt.NDArray[np.bool_], length: int):
4747
"""
4848
Validate the size of the values passed to ExtensionArray.fillna.
4949
"""

0 commit comments

Comments
 (0)