27
27
F ,
28
28
Scalar ,
29
29
Shape ,
30
+ npt ,
30
31
)
31
32
from pandas .compat ._optional import import_optional_dependency
32
33
33
34
from pandas .core .dtypes .common import (
34
- get_dtype ,
35
35
is_any_int_dtype ,
36
36
is_bool_dtype ,
37
37
is_complex ,
@@ -209,8 +209,8 @@ def _get_fill_value(
209
209
210
210
211
211
def _maybe_get_mask (
212
- values : np .ndarray , skipna : bool , mask : np .ndarray | None
213
- ) -> np .ndarray | None :
212
+ values : np .ndarray , skipna : bool , mask : npt . NDArray [ np .bool_ ] | None
213
+ ) -> npt . NDArray [ np .bool_ ] | None :
214
214
"""
215
215
Compute a mask if and only if necessary.
216
216
@@ -239,7 +239,7 @@ def _maybe_get_mask(
239
239
240
240
Returns
241
241
-------
242
- Optional[np.ndarray]
242
+ Optional[np.ndarray[bool] ]
243
243
"""
244
244
if mask is None :
245
245
if is_bool_dtype (values .dtype ) or is_integer_dtype (values .dtype ):
@@ -257,8 +257,8 @@ def _get_values(
257
257
skipna : bool ,
258
258
fill_value : Any = None ,
259
259
fill_value_typ : str | None = None ,
260
- mask : np .ndarray | None = None ,
261
- ) -> tuple [np .ndarray , np .ndarray | None , np .dtype , np .dtype , Any ]:
260
+ mask : npt . NDArray [ np .bool_ ] | None = None ,
261
+ ) -> tuple [np .ndarray , npt . NDArray [ np .bool_ ] | None , np .dtype , np .dtype , Any ]:
262
262
"""
263
263
Utility to get the values view, mask, dtype, dtype_max, and fill_value.
264
264
@@ -279,7 +279,7 @@ def _get_values(
279
279
value to fill NaNs with
280
280
fill_value_typ : str
281
281
Set to '+inf' or '-inf' to handle dtype-specific infinities
282
- mask : Optional[np.ndarray]
282
+ mask : Optional[np.ndarray[bool] ]
283
283
nan-mask if known
284
284
285
285
Returns
@@ -396,7 +396,7 @@ def new_func(
396
396
* ,
397
397
axis : int | None = None ,
398
398
skipna : bool = True ,
399
- mask : np .ndarray | None = None ,
399
+ mask : npt . NDArray [ np .bool_ ] | None = None ,
400
400
** kwargs ,
401
401
):
402
402
orig_values = values
@@ -454,7 +454,7 @@ def nanany(
454
454
* ,
455
455
axis : int | None = None ,
456
456
skipna : bool = True ,
457
- mask : np .ndarray | None = None ,
457
+ mask : npt . NDArray [ np .bool_ ] | None = None ,
458
458
) -> bool :
459
459
"""
460
460
Check if any elements along an axis evaluate to True.
@@ -500,7 +500,7 @@ def nanall(
500
500
* ,
501
501
axis : int | None = None ,
502
502
skipna : bool = True ,
503
- mask : np .ndarray | None = None ,
503
+ mask : npt . NDArray [ np .bool_ ] | None = None ,
504
504
) -> bool :
505
505
"""
506
506
Check if all elements along an axis evaluate to True.
@@ -549,7 +549,7 @@ def nansum(
549
549
axis : int | None = None ,
550
550
skipna : bool = True ,
551
551
min_count : int = 0 ,
552
- mask : np .ndarray | None = None ,
552
+ mask : npt . NDArray [ np .bool_ ] | None = None ,
553
553
) -> float :
554
554
"""
555
555
Sum the elements along an axis ignoring NaNs
@@ -592,7 +592,7 @@ def nansum(
592
592
def _mask_datetimelike_result (
593
593
result : np .ndarray | np .datetime64 | np .timedelta64 ,
594
594
axis : int | None ,
595
- mask : np .ndarray ,
595
+ mask : npt . NDArray [ np .bool_ ] ,
596
596
orig_values : np .ndarray ,
597
597
) -> np .ndarray | np .datetime64 | np .timedelta64 | NaTType :
598
598
if isinstance (result , np .ndarray ):
@@ -616,7 +616,7 @@ def nanmean(
616
616
* ,
617
617
axis : int | None = None ,
618
618
skipna : bool = True ,
619
- mask : np .ndarray | None = None ,
619
+ mask : npt . NDArray [ np .bool_ ] | None = None ,
620
620
) -> float :
621
621
"""
622
622
Compute the mean of the element along an axis ignoring NaNs
@@ -781,10 +781,10 @@ def get_empty_reduction_result(
781
781
782
782
def _get_counts_nanvar (
783
783
values_shape : Shape ,
784
- mask : np .ndarray | None ,
784
+ mask : npt . NDArray [ np .bool_ ] | None ,
785
785
axis : int | None ,
786
786
ddof : int ,
787
- dtype : Dtype = float ,
787
+ dtype : np . dtype = np . dtype ( np . float64 ) ,
788
788
) -> tuple [int | float | np .ndarray , int | float | np .ndarray ]:
789
789
"""
790
790
Get the count of non-null values along an axis, accounting
@@ -808,7 +808,6 @@ def _get_counts_nanvar(
808
808
count : int, np.nan or np.ndarray
809
809
d : int, np.nan or np.ndarray
810
810
"""
811
- dtype = get_dtype (dtype )
812
811
count = _get_counts (values_shape , mask , axis , dtype = dtype )
813
812
d = count - dtype .type (ddof )
814
813
@@ -931,7 +930,7 @@ def nanvar(values, *, axis=None, skipna=True, ddof=1, mask=None):
931
930
# unless we were dealing with a float array, in which case use the same
932
931
# precision as the original values array.
933
932
if is_float_dtype (dtype ):
934
- result = result .astype (dtype )
933
+ result = result .astype (dtype , copy = False )
935
934
return result
936
935
937
936
@@ -942,7 +941,7 @@ def nansem(
942
941
axis : int | None = None ,
943
942
skipna : bool = True ,
944
943
ddof : int = 1 ,
945
- mask : np .ndarray | None = None ,
944
+ mask : npt . NDArray [ np .bool_ ] | None = None ,
946
945
) -> float :
947
946
"""
948
947
Compute the standard error in the mean along given axis while ignoring NaNs
@@ -993,7 +992,7 @@ def reduction(
993
992
* ,
994
993
axis : int | None = None ,
995
994
skipna : bool = True ,
996
- mask : np .ndarray | None = None ,
995
+ mask : npt . NDArray [ np .bool_ ] | None = None ,
997
996
) -> Dtype :
998
997
999
998
values , mask , dtype , dtype_max , fill_value = _get_values (
@@ -1025,7 +1024,7 @@ def nanargmax(
1025
1024
* ,
1026
1025
axis : int | None = None ,
1027
1026
skipna : bool = True ,
1028
- mask : np .ndarray | None = None ,
1027
+ mask : npt . NDArray [ np .bool_ ] | None = None ,
1029
1028
) -> int | np .ndarray :
1030
1029
"""
1031
1030
Parameters
@@ -1071,7 +1070,7 @@ def nanargmin(
1071
1070
* ,
1072
1071
axis : int | None = None ,
1073
1072
skipna : bool = True ,
1074
- mask : np .ndarray | None = None ,
1073
+ mask : npt . NDArray [ np .bool_ ] | None = None ,
1075
1074
) -> int | np .ndarray :
1076
1075
"""
1077
1076
Parameters
@@ -1117,7 +1116,7 @@ def nanskew(
1117
1116
* ,
1118
1117
axis : int | None = None ,
1119
1118
skipna : bool = True ,
1120
- mask : np .ndarray | None = None ,
1119
+ mask : npt . NDArray [ np .bool_ ] | None = None ,
1121
1120
) -> float :
1122
1121
"""
1123
1122
Compute the sample skewness.
@@ -1185,7 +1184,7 @@ def nanskew(
1185
1184
1186
1185
dtype = values .dtype
1187
1186
if is_float_dtype (dtype ):
1188
- result = result .astype (dtype )
1187
+ result = result .astype (dtype , copy = False )
1189
1188
1190
1189
if isinstance (result , np .ndarray ):
1191
1190
result = np .where (m2 == 0 , 0 , result )
@@ -1204,7 +1203,7 @@ def nankurt(
1204
1203
* ,
1205
1204
axis : int | None = None ,
1206
1205
skipna : bool = True ,
1207
- mask : np .ndarray | None = None ,
1206
+ mask : npt . NDArray [ np .bool_ ] | None = None ,
1208
1207
) -> float :
1209
1208
"""
1210
1209
Compute the sample excess kurtosis
@@ -1285,7 +1284,7 @@ def nankurt(
1285
1284
1286
1285
dtype = values .dtype
1287
1286
if is_float_dtype (dtype ):
1288
- result = result .astype (dtype )
1287
+ result = result .astype (dtype , copy = False )
1289
1288
1290
1289
if isinstance (result , np .ndarray ):
1291
1290
result = np .where (denominator == 0 , 0 , result )
@@ -1301,7 +1300,7 @@ def nanprod(
1301
1300
axis : int | None = None ,
1302
1301
skipna : bool = True ,
1303
1302
min_count : int = 0 ,
1304
- mask : np .ndarray | None = None ,
1303
+ mask : npt . NDArray [ np .bool_ ] | None = None ,
1305
1304
) -> float :
1306
1305
"""
1307
1306
Parameters
@@ -1339,7 +1338,10 @@ def nanprod(
1339
1338
1340
1339
1341
1340
def _maybe_arg_null_out (
1342
- result : np .ndarray , axis : int | None , mask : np .ndarray | None , skipna : bool
1341
+ result : np .ndarray ,
1342
+ axis : int | None ,
1343
+ mask : npt .NDArray [np .bool_ ] | None ,
1344
+ skipna : bool ,
1343
1345
) -> np .ndarray | int :
1344
1346
# helper function for nanargmin/nanargmax
1345
1347
if mask is None :
@@ -1367,10 +1369,10 @@ def _maybe_arg_null_out(
1367
1369
1368
1370
1369
1371
def _get_counts (
1370
- values_shape : tuple [ int , ...] ,
1371
- mask : np .ndarray | None ,
1372
+ values_shape : Shape ,
1373
+ mask : npt . NDArray [ np .bool_ ] | None ,
1372
1374
axis : int | None ,
1373
- dtype : Dtype = float ,
1375
+ dtype : np . dtype = np . dtype ( np . float64 ) ,
1374
1376
) -> int | float | np .ndarray :
1375
1377
"""
1376
1378
Get the count of non-null values along an axis
@@ -1390,7 +1392,6 @@ def _get_counts(
1390
1392
-------
1391
1393
count : scalar or array
1392
1394
"""
1393
- dtype = get_dtype (dtype )
1394
1395
if axis is None :
1395
1396
if mask is not None :
1396
1397
n = mask .size - mask .sum ()
@@ -1405,20 +1406,13 @@ def _get_counts(
1405
1406
1406
1407
if is_scalar (count ):
1407
1408
return dtype .type (count )
1408
- try :
1409
- return count .astype (dtype )
1410
- except AttributeError :
1411
- # error: Argument "dtype" to "array" has incompatible type
1412
- # "Union[ExtensionDtype, dtype]"; expected "Union[dtype, None, type,
1413
- # _SupportsDtype, str, Tuple[Any, int], Tuple[Any, Union[int,
1414
- # Sequence[int]]], List[Any], _DtypeDict, Tuple[Any, Any]]"
1415
- return np .array (count , dtype = dtype ) # type: ignore[arg-type]
1409
+ return count .astype (dtype , copy = False )
1416
1410
1417
1411
1418
1412
def _maybe_null_out (
1419
1413
result : np .ndarray | float | NaTType ,
1420
1414
axis : int | None ,
1421
- mask : np .ndarray | None ,
1415
+ mask : npt . NDArray [ np .bool_ ] | None ,
1422
1416
shape : tuple [int , ...],
1423
1417
min_count : int = 1 ,
1424
1418
) -> np .ndarray | float | NaTType :
@@ -1455,7 +1449,7 @@ def _maybe_null_out(
1455
1449
1456
1450
1457
1451
def check_below_min_count (
1458
- shape : tuple [int , ...], mask : np .ndarray | None , min_count : int
1452
+ shape : tuple [int , ...], mask : npt . NDArray [ np .bool_ ] | None , min_count : int
1459
1453
) -> bool :
1460
1454
"""
1461
1455
Check for the `min_count` keyword. Returns True if below `min_count` (when
@@ -1465,7 +1459,7 @@ def check_below_min_count(
1465
1459
----------
1466
1460
shape : tuple
1467
1461
The shape of the values (`values.shape`).
1468
- mask : ndarray or None
1462
+ mask : ndarray[bool] or None
1469
1463
Boolean numpy array (typically of same shape as `shape`) or None.
1470
1464
min_count : int
1471
1465
Keyword passed through from sum/prod call.
@@ -1634,7 +1628,11 @@ def f(x, y):
1634
1628
1635
1629
1636
1630
def _nanpercentile_1d (
1637
- values : np .ndarray , mask : np .ndarray , q : np .ndarray , na_value : Scalar , interpolation
1631
+ values : np .ndarray ,
1632
+ mask : npt .NDArray [np .bool_ ],
1633
+ q : np .ndarray ,
1634
+ na_value : Scalar ,
1635
+ interpolation ,
1638
1636
) -> Scalar | np .ndarray :
1639
1637
"""
1640
1638
Wrapper for np.percentile that skips missing values, specialized to
@@ -1668,7 +1666,7 @@ def nanpercentile(
1668
1666
q : np .ndarray ,
1669
1667
* ,
1670
1668
na_value ,
1671
- mask : np .ndarray ,
1669
+ mask : npt . NDArray [ np .bool_ ] ,
1672
1670
interpolation ,
1673
1671
):
1674
1672
"""
0 commit comments