forked from pandas-dev/pandas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmasked_reductions.py
47 lines (40 loc) · 1.33 KB
/
masked_reductions.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
"""
masked_reductions.py is for reduction algorithms using a mask-based approach
for missing values.
"""
import numpy as np
from pandas._libs import missing as libmissing
from pandas.compat.numpy import _np_version_under1p17
from pandas.core.nanops import _below_min_count
def sum(
values: np.ndarray, mask: np.ndarray, skipna: bool, min_count: int = 0,
):
"""
Sum for 1D masked array.
Parameters
----------
values : np.ndarray
Numpy array with the values (can be of any dtype that support the
operation).
mask : np.ndarray
Boolean numpy array (True values indicate missing values).
skipna : bool, default True
Whether to skip NA.
min_count : int, default 0
The required number of valid values to perform the operation. If fewer than
``min_count`` non-NA values are present the result will be NA.
"""
if not skipna:
if mask.any():
return libmissing.NA
else:
if _below_min_count(values.shape, None, min_count):
return libmissing.NA
return np.sum(values)
else:
if _below_min_count(values.shape, mask, min_count):
return libmissing.NA
if _np_version_under1p17:
return np.sum(values[~mask])
else:
return np.sum(values, where=~mask)