Skip to content

Commit 4f9c8eb

Browse files
committed
Incorporate comments
1 parent d8d1f50 commit 4f9c8eb

File tree

1 file changed

+29
-24
lines changed

1 file changed

+29
-24
lines changed

pandas/core/nanops.py

+29-24
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import functools
33
import itertools
44
import operator
5+
from typing import Any, Optional, Tuple
56
import warnings
67

78
import numpy as np
@@ -200,7 +201,8 @@ def _get_fill_value(dtype, fill_value=None, fill_value_typ=None):
200201
return tslibs.iNaT
201202

202203

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]:
204206
""" This function will compute a mask iff it is necessary. Otherwise,
205207
return the provided mask (potentially None) when a mask does not need to be
206208
computed.
@@ -217,16 +219,16 @@ def _maybe_get_mask(values, skipna, mask, invert=False):
217219
218220
Parameters
219221
----------
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 :
223227
nan-mask if known
224-
invert : bool, optional
225-
return notna() instead of isna()
226228
227229
Returns
228230
-------
229-
result : Optional[ndarray[bool]]
231+
Optional[np.ndarray]
230232
231233
"""
232234

@@ -236,16 +238,15 @@ def _maybe_get_mask(values, skipna, mask, invert=False):
236238
return None
237239

238240
if skipna:
239-
if not invert:
240-
mask = isna(values)
241-
else:
242-
mask = notna(values)
241+
mask = isna(values)
243242

244243
return mask
245244

246245

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]:
249250
""" Utility to get the values view, mask, dtype, dtype_max, and fill_value.
250251
251252
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,
257258
258259
Parameters
259260
----------
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 :
263266
value to fill NaNs with
264-
fill_value_typ : str, optional
267+
fill_value_typ :
265268
Set to '+inf' or '-inf' to handle dtype-specific infinities
266-
mask : Optional[ndarray[bool]]
269+
mask :
267270
nan-mask if known
268271
269272
Returns
@@ -274,8 +277,10 @@ def _get_values(values, skipna, fill_value=None, fill_value_typ=None,
274277
Mask for values, if deemed necessary to compute
275278
dtype : dtype
276279
dtype for values
277-
dtype_max :
278-
280+
dtype_max : dtype
281+
platform independent dtype
282+
fill_value : Any
283+
fill value used
279284
"""
280285
mask = _maybe_get_mask(values, skipna, mask)
281286

@@ -582,7 +587,7 @@ def nanmedian(values, axis=None, skipna=True, mask=None):
582587
2.0
583588
"""
584589
def get_median(x):
585-
mask = _maybe_get_mask(x, skipna, None, invert=True)
590+
mask = notna(x)
586591
if mask is not None:
587592
if not skipna and not mask.all():
588593
return np.nan
@@ -1100,7 +1105,7 @@ def nanprod(values, axis=None, skipna=True, min_count=0, mask=None):
11001105
"""
11011106
mask = _maybe_get_mask(values, skipna, mask)
11021107

1103-
if skipna and not is_any_int_dtype(values) and mask is not None:
1108+
if skipna and mask is not None:
11041109
values = values.copy()
11051110
values[mask] = 1
11061111
result = values.prod(axis)
@@ -1153,7 +1158,7 @@ def _get_counts(values_shape, mask, axis, dtype=float):
11531158
if mask is not None:
11541159
n = mask.size - mask.sum()
11551160
else:
1156-
n = functools.reduce(operator.mul, values_shape)
1161+
n = np.prod(values_shape)
11571162
return dtype.type(n)
11581163

11591164
if mask is not None:
@@ -1187,7 +1192,7 @@ def _maybe_null_out(result, axis, mask, shape, min_count=1):
11871192
if mask is not None:
11881193
null_mask = mask.size - mask.sum()
11891194
else:
1190-
null_mask = functools.reduce(operator.mul, shape)
1195+
null_mask = np.prod(shape)
11911196
if null_mask < min_count:
11921197
result = np.nan
11931198

0 commit comments

Comments
 (0)