2
2
import functools
3
3
import itertools
4
4
import operator
5
+ from typing import Any , Optional , Tuple
5
6
import warnings
6
7
7
8
import numpy as np
@@ -200,7 +201,8 @@ def _get_fill_value(dtype, fill_value=None, fill_value_typ=None):
200
201
return tslibs .iNaT
201
202
202
203
203
- def _maybe_get_mask (values , skipna , mask , invert = False ):
204
+ def _maybe_get_mask (values : np .ndarray , skipna : bool ,
205
+ mask : Optional [np .ndarray ]) -> Optional [np .ndarray ]:
204
206
""" This function will compute a mask iff it is necessary. Otherwise,
205
207
return the provided mask (potentially None) when a mask does not need to be
206
208
computed.
@@ -217,16 +219,16 @@ def _maybe_get_mask(values, skipna, mask, invert=False):
217
219
218
220
Parameters
219
221
----------
220
- values : ndarray
221
- skipna : bool
222
- mask : Optional[ndarray[bool]]
222
+ values :
223
+ input array to potentially compute mask for
224
+ skipna :
225
+ boolean for whether NaNs should be skipped
226
+ mask :
223
227
nan-mask if known
224
- invert : bool, optional
225
- return notna() instead of isna()
226
228
227
229
Returns
228
230
-------
229
- result : Optional[ndarray[bool] ]
231
+ Optional[np. ndarray]
230
232
231
233
"""
232
234
@@ -236,16 +238,15 @@ def _maybe_get_mask(values, skipna, mask, invert=False):
236
238
return None
237
239
238
240
if skipna :
239
- if not invert :
240
- mask = isna (values )
241
- else :
242
- mask = notna (values )
241
+ mask = isna (values )
243
242
244
243
return mask
245
244
246
245
247
- def _get_values (values , skipna , fill_value = None , fill_value_typ = None ,
248
- mask = None ):
246
+ def _get_values (values : np .ndarray , skipna : bool , fill_value : Any = None ,
247
+ fill_value_typ : str = None , mask : Optional [np .ndarray ] = None
248
+ ) -> Tuple [np .ndarray , Optional [np .ndarray ], np .dtype ,
249
+ np .dtype , Any ]:
249
250
""" Utility to get the values view, mask, dtype, dtype_max, and fill_value.
250
251
251
252
If both mask and fill_value/fill_value_typ are not None and skipna is True,
@@ -257,13 +258,15 @@ def _get_values(values, skipna, fill_value=None, fill_value_typ=None,
257
258
258
259
Parameters
259
260
----------
260
- values : ndarray
261
- skipna : bool
262
- fill_value : Any, optional
261
+ values :
262
+ input array to potentially compute mask for
263
+ skipna :
264
+ boolean for whether NaNs should be skipped
265
+ fill_value :
263
266
value to fill NaNs with
264
- fill_value_typ : str, optional
267
+ fill_value_typ :
265
268
Set to '+inf' or '-inf' to handle dtype-specific infinities
266
- mask : Optional[ndarray[bool]]
269
+ mask :
267
270
nan-mask if known
268
271
269
272
Returns
@@ -274,8 +277,10 @@ def _get_values(values, skipna, fill_value=None, fill_value_typ=None,
274
277
Mask for values, if deemed necessary to compute
275
278
dtype : dtype
276
279
dtype for values
277
- dtype_max :
278
-
280
+ dtype_max : dtype
281
+ platform independent dtype
282
+ fill_value : Any
283
+ fill value used
279
284
"""
280
285
mask = _maybe_get_mask (values , skipna , mask )
281
286
@@ -582,7 +587,7 @@ def nanmedian(values, axis=None, skipna=True, mask=None):
582
587
2.0
583
588
"""
584
589
def get_median (x ):
585
- mask = _maybe_get_mask ( x , skipna , None , invert = True )
590
+ mask = notna ( x )
586
591
if mask is not None :
587
592
if not skipna and not mask .all ():
588
593
return np .nan
@@ -1100,7 +1105,7 @@ def nanprod(values, axis=None, skipna=True, min_count=0, mask=None):
1100
1105
"""
1101
1106
mask = _maybe_get_mask (values , skipna , mask )
1102
1107
1103
- if skipna and not is_any_int_dtype ( values ) and mask is not None :
1108
+ if skipna and mask is not None :
1104
1109
values = values .copy ()
1105
1110
values [mask ] = 1
1106
1111
result = values .prod (axis )
@@ -1153,7 +1158,7 @@ def _get_counts(values_shape, mask, axis, dtype=float):
1153
1158
if mask is not None :
1154
1159
n = mask .size - mask .sum ()
1155
1160
else :
1156
- n = functools . reduce ( operator . mul , values_shape )
1161
+ n = np . prod ( values_shape )
1157
1162
return dtype .type (n )
1158
1163
1159
1164
if mask is not None :
@@ -1187,7 +1192,7 @@ def _maybe_null_out(result, axis, mask, shape, min_count=1):
1187
1192
if mask is not None :
1188
1193
null_mask = mask .size - mask .sum ()
1189
1194
else :
1190
- null_mask = functools . reduce ( operator . mul , shape )
1195
+ null_mask = np . prod ( shape )
1191
1196
if null_mask < min_count :
1192
1197
result = np .nan
1193
1198
0 commit comments