24
24
from numpy.lib.function_base import _ureduce as _ureduce
27
28
'nansum', 'nanmax', 'nanmin', 'nanargmax', 'nanargmin', 'nanmean',
28
29
'nanmedian', 'nanpercentile', 'nanvar', 'nanstd', 'nanprod',
141
142
return np.divide(a, b, out=out, casting='unsafe')
144
def nanmin(a, axis=None, out=None, keepdims=False):
145
def nanmin(a, axis=None, out=None, keepdims=np._NoValue):
146
147
Return minimum of an array or minimum along an axis, ignoring any NaNs.
147
148
When all-NaN slices are encountered a ``RuntimeWarning`` is raised and
164
165
.. versionadded:: 1.8.0
165
166
keepdims : bool, optional
166
If this is set to True, the axes which are reduced are left in the
167
result as dimensions with size one. With this option, the result
168
will broadcast correctly against the original `a`.
167
If this is set to True, the axes which are reduced are left
168
in the result as dimensions with size one. With this option,
169
the result will broadcast correctly against the original `a`.
171
If the value is anything but the default, then
172
`keepdims` will be passed through to the `min` method
173
of sub-classes of `ndarray`. If the sub-classes methods
174
does not implement `keepdims` any exceptions will be raised.
170
176
.. versionadded:: 1.8.0
230
if keepdims is not np._NoValue:
231
kwargs['keepdims'] = keepdims
223
232
if not isinstance(a, np.ndarray) or type(a) is np.ndarray:
224
233
# Fast, but not safe for subclasses of ndarray
225
res = np.fmin.reduce(a, axis=axis, out=out, keepdims=keepdims)
234
res = np.fmin.reduce(a, axis=axis, out=out, **kwargs)
226
235
if np.isnan(res).any():
227
236
warnings.warn("All-NaN axis encountered", RuntimeWarning)
229
238
# Slow, but safe for subclasses of ndarray
230
239
a, mask = _replace_nan(a, +np.inf)
231
res = np.amin(a, axis=axis, out=out, keepdims=keepdims)
240
res = np.amin(a, axis=axis, out=out, **kwargs)
235
244
# Check for all-NaN axis
236
mask = np.all(mask, axis=axis, keepdims=keepdims)
245
mask = np.all(mask, axis=axis, **kwargs)
238
247
res = _copyto(res, np.nan, mask)
239
248
warnings.warn("All-NaN axis encountered", RuntimeWarning)
243
def nanmax(a, axis=None, out=None, keepdims=False):
252
def nanmax(a, axis=None, out=None, keepdims=np._NoValue):
245
254
Return the maximum of an array or maximum along an axis, ignoring any
246
255
NaNs. When all-NaN slices are encountered a ``RuntimeWarning`` is
263
272
.. versionadded:: 1.8.0
264
273
keepdims : bool, optional
265
If this is set to True, the axes which are reduced are left in the
266
result as dimensions with size one. With this option, the result
267
will broadcast correctly against the original `a`.
274
If this is set to True, the axes which are reduced are left
275
in the result as dimensions with size one. With this option,
276
the result will broadcast correctly against the original `a`.
278
If the value is anything but the default, then
279
`keepdims` will be passed through to the `max` method
280
of sub-classes of `ndarray`. If the sub-classes methods
281
does not implement `keepdims` any exceptions will be raised.
269
283
.. versionadded:: 1.8.0
337
if keepdims is not np._NoValue:
338
kwargs['keepdims'] = keepdims
322
339
if not isinstance(a, np.ndarray) or type(a) is np.ndarray:
323
340
# Fast, but not safe for subclasses of ndarray
324
res = np.fmax.reduce(a, axis=axis, out=out, keepdims=keepdims)
341
res = np.fmax.reduce(a, axis=axis, out=out, **kwargs)
325
342
if np.isnan(res).any():
326
343
warnings.warn("All-NaN slice encountered", RuntimeWarning)
328
345
# Slow, but safe for subclasses of ndarray
329
346
a, mask = _replace_nan(a, -np.inf)
330
res = np.amax(a, axis=axis, out=out, keepdims=keepdims)
347
res = np.amax(a, axis=axis, out=out, **kwargs)
334
351
# Check for all-NaN axis
335
mask = np.all(mask, axis=axis, keepdims=keepdims)
352
mask = np.all(mask, axis=axis, **kwargs)
337
354
res = _copyto(res, np.nan, mask)
338
355
warnings.warn("All-NaN axis encountered", RuntimeWarning)
431
def nansum(a, axis=None, dtype=None, out=None, keepdims=0):
448
def nansum(a, axis=None, dtype=None, out=None, keepdims=np._NoValue):
433
450
Return the sum of array elements over a given axis treating Not a
434
451
Numbers (NaNs) as zero.
463
480
.. versionadded:: 1.8.0
464
481
keepdims : bool, optional
465
If True, the axes which are reduced are left in the result as
466
dimensions with size one. With this option, the result will
467
broadcast correctly against the original `arr`.
482
If this is set to True, the axes which are reduced are left
483
in the result as dimensions with size one. With this option,
484
the result will broadcast correctly against the original `a`.
487
If the value is anything but the default, then
488
`keepdims` will be passed through to the `mean` or `sum` methods
489
of sub-classes of `ndarray`. If the sub-classes methods
490
does not implement `keepdims` any exceptions will be raised.
469
492
.. versionadded:: 1.8.0
513
536
return np.sum(a, axis=axis, dtype=dtype, out=out, keepdims=keepdims)
516
def nanprod(a, axis=None, dtype=None, out=None, keepdims=0):
539
def nanprod(a, axis=None, dtype=None, out=None, keepdims=np._NoValue):
518
541
Return the product of array elements over a given axis treating Not a
519
542
Numbers (NaNs) as zero.
583
606
return np.prod(a, axis=axis, dtype=dtype, out=out, keepdims=keepdims)
586
def nanmean(a, axis=None, dtype=None, out=None, keepdims=False):
609
def nanmean(a, axis=None, dtype=None, out=None, keepdims=np._NoValue):
588
611
Compute the arithmetic mean along the specified axis, ignoring NaNs.
613
636
expected output, but the type will be cast if necessary. See
614
637
`doc.ufuncs` for details.
615
638
keepdims : bool, optional
616
If this is set to True, the axes which are reduced are left in the
617
result as dimensions with size one. With this option, the result
618
will broadcast correctly against the original `arr`.
639
If this is set to True, the axes which are reduced are left
640
in the result as dimensions with size one. With this option,
641
the result will broadcast correctly against the original `a`.
643
If the value is anything but the default, then
644
`keepdims` will be passed through to the `mean` or `sum` methods
645
of sub-classes of `ndarray`. If the sub-classes methods
646
does not implement `keepdims` any exceptions will be raised.
744
773
return m.filled(np.nan)
746
def nanmedian(a, axis=None, out=None, overwrite_input=False, keepdims=False):
776
def nanmedian(a, axis=None, out=None, overwrite_input=False, keepdims=np._NoValue):
748
778
Compute the median along the specified axis, while ignoring NaNs.
772
802
False. If `overwrite_input` is ``True`` and `a` is not already an
773
803
`ndarray`, an error will be raised.
774
804
keepdims : bool, optional
775
If this is set to True, the axes which are reduced are left in
776
the result as dimensions with size one. With this option, the
777
result will broadcast correctly against the original `arr`.
805
If this is set to True, the axes which are reduced are left
806
in the result as dimensions with size one. With this option,
807
the result will broadcast correctly against the original `a`.
809
If this is anything but the default value it will be passed
810
through (in the special case of an empty array) to the
811
`mean` function of the underlying array. If the array is
812
a sub-class and `mean` does not have the kwarg `keepdims` this
813
will raise a RuntimeError.
830
866
r, k = _ureduce(a, func=_nanmedian, axis=axis, out=out,
831
867
overwrite_input=overwrite_input)
868
if keepdims and keepdims is not np._NoValue:
833
869
return r.reshape(k)
838
874
def nanpercentile(a, q, axis=None, out=None, overwrite_input=False,
839
interpolation='linear', keepdims=False):
875
interpolation='linear', keepdims=np._NoValue):
841
877
Compute the qth percentile of the data along the specified axis,
842
878
while ignoring nan values.
883
919
* nearest: ``i`` or ``j``, whichever is nearest.
884
920
* midpoint: ``(i + j) / 2``.
885
921
keepdims : bool, optional
886
If this is set to True, the axes which are reduced are left in
887
the result as dimensions with size one. With this option, the
888
result will broadcast correctly against the original array `a`.
922
If this is set to True, the axes which are reduced are left
923
in the result as dimensions with size one. With this option,
924
the result will broadcast correctly against the original `a`.
926
If this is anything but the default value it will be passed
927
through (in the special case of an empty array) to the
928
`mean` function of the underlying array. If the array is
929
a sub-class and `mean` does not have the kwarg `keepdims` this
930
will raise a RuntimeError.
893
935
If `q` is a single percentile and `axis=None`, then the result
894
936
is a scalar. If multiple percentiles are given, first axis of
895
937
the result corresponds to the percentiles. The other axes are
896
the axes that remain after the reduction of `a`. If the input
938
the axes that remain after the reduction of `a`. If the input
897
939
contains integers or floats smaller than ``float64``, the output
898
940
data-type is ``float64``. Otherwise, the output data-type is the
899
941
same as that of the input. If `out` is specified, that array is
954
996
r, k = _ureduce(a, func=_nanpercentile, q=q, axis=axis, out=out,
955
997
overwrite_input=overwrite_input,
956
998
interpolation=interpolation)
999
if keepdims and keepdims is not np._NoValue:
959
1001
return r.reshape(k)
966
1008
def _nanpercentile(a, q, axis=None, out=None, overwrite_input=False,
967
interpolation='linear', keepdims=False):
1009
interpolation='linear'):
969
1011
Private function that doesn't support extended axis or keepdims.
970
1012
These methods are extended to this function using _ureduce
1020
1062
interpolation=interpolation)
1023
def nanvar(a, axis=None, dtype=None, out=None, ddof=0, keepdims=False):
1065
def nanvar(a, axis=None, dtype=None, out=None, ddof=0, keepdims=np._NoValue):
1025
1067
Compute the variance along the specified axis, while ignoring NaNs.
1056
1098
keepdims : bool, optional
1057
1099
If this is set to True, the axes which are reduced are left
1058
1100
in the result as dimensions with size one. With this option,
1059
the result will broadcast correctly against the original `arr`.
1101
the result will broadcast correctly against the original `a`.
1095
1138
below). Specifying a higher-accuracy accumulator using the ``dtype``
1096
1139
keyword can alleviate this issue.
1141
For this function to work on sub-classes of ndarray, they must define
1142
`sum` with the kwarg `keepdims`
1100
1146
>>> a = np.array([[1, np.nan], [3, 4]])
1122
1168
warnings.simplefilter('ignore')
1125
cnt = np.sum(~mask, axis=axis, dtype=np.intp, keepdims=True)
1126
avg = np.sum(arr, axis=axis, dtype=dtype, keepdims=True)
1171
if type(arr) is np.matrix:
1172
_keepdims = np._NoValue
1175
# we need to special case matrix for reverse compatibility
1176
# in order for this to work, these sums need to be called with
1177
# keepdims=True, however matrix now raises an error in this case, but
1178
# the reason that it drops the keepdims kwarg is to force keepdims=True
1179
# so this used to work by serendipity.
1180
cnt = np.sum(~mask, axis=axis, dtype=np.intp, keepdims=_keepdims)
1181
avg = np.sum(arr, axis=axis, dtype=dtype, keepdims=_keepdims)
1127
1182
avg = _divide_by_count(avg, cnt)
1129
1184
# Compute squared deviation from mean.
1154
def nanstd(a, axis=None, dtype=None, out=None, ddof=0, keepdims=False):
1209
def nanstd(a, axis=None, dtype=None, out=None, ddof=0, keepdims=np._NoValue):
1156
1211
Compute the standard deviation along the specified axis, while
1185
1240
Means Delta Degrees of Freedom. The divisor used in calculations
1186
1241
is ``N - ddof``, where ``N`` represents the number of non-NaN
1187
1242
elements. By default `ddof` is zero.
1188
1244
keepdims : bool, optional
1189
1245
If this is set to True, the axes which are reduced are left
1190
1246
in the result as dimensions with size one. With this option,
1191
the result will broadcast correctly against the original `arr`.
1247
the result will broadcast correctly against the original `a`.
1249
If this value is anything but the default it is passed through
1250
as-is to the relevant functions of the sub-classes. If these
1251
functions do not have a `keepdims` kwarg, a RuntimeError will