summaryrefslogtreecommitdiff
path: root/numpy/lib/nanfunctions.py
diff options
context:
space:
mode:
Diffstat (limited to 'numpy/lib/nanfunctions.py')
-rw-r--r--numpy/lib/nanfunctions.py80
1 files changed, 42 insertions, 38 deletions
diff --git a/numpy/lib/nanfunctions.py b/numpy/lib/nanfunctions.py
index b3f3bfc69..4b2c9d817 100644
--- a/numpy/lib/nanfunctions.py
+++ b/numpy/lib/nanfunctions.py
@@ -23,7 +23,7 @@ from __future__ import division, absolute_import, print_function
import warnings
import numpy as np
-from numpy.lib.function_base import _ureduce as _ureduce
+from numpy.lib import function_base
__all__ = [
@@ -497,7 +497,7 @@ def nansum(a, axis=None, dtype=None, out=None, keepdims=np._NoValue):
Return the sum of array elements over a given axis treating Not a
Numbers (NaNs) as zero.
- In NumPy versions <= 1.8.0 Nan is returned for slices that are all-NaN or
+ In NumPy versions <= 1.9.0 Nan is returned for slices that are all-NaN or
empty. In later versions zero is returned.
Parameters
@@ -1017,8 +1017,8 @@ def nanmedian(a, axis=None, out=None, overwrite_input=False, keepdims=np._NoValu
if a.size == 0:
return np.nanmean(a, axis, out=out, keepdims=keepdims)
- r, k = _ureduce(a, func=_nanmedian, axis=axis, out=out,
- overwrite_input=overwrite_input)
+ r, k = function_base._ureduce(a, func=_nanmedian, axis=axis, out=out,
+ overwrite_input=overwrite_input)
if keepdims and keepdims is not np._NoValue:
return r.reshape(k)
else:
@@ -1038,36 +1038,30 @@ def nanpercentile(a, q, axis=None, out=None, overwrite_input=False,
Parameters
----------
a : array_like
- Input array or object that can be converted to an array.
- q : float in range of [0,100] (or sequence of floats)
- Percentile to compute, which must be between 0 and 100
- inclusive.
- axis : {int, sequence of int, None}, optional
+ Input array or object that can be converted to an array, containing
+ nan values to be ignored.
+ q : array_like of float
+ Percentile or sequence of percentiles to compute, which must be between
+ 0 and 100 inclusive.
+ axis : {int, tuple of int, None}, optional
Axis or axes along which the percentiles are computed. The
default is to compute the percentile(s) along a flattened
- version of the array. A sequence of axes is supported since
- version 1.9.0.
+ version of the array.
out : ndarray, optional
Alternative output array in which to place the result. It must
have the same shape and buffer length as the expected output,
but the type (of the output) will be cast if necessary.
overwrite_input : bool, optional
- If True, then allow use of memory of input array `a` for
- calculations. The input array will be modified by the call to
- `percentile`. This will save memory when you do not need to
- preserve the contents of the input array. In this case you
- should not make any assumptions about the contents of the input
- `a` after this function completes -- treat it as undefined.
- Default is False. If `a` is not already an array, this parameter
- will have no effect as `a` will be converted to an array
- internally regardless of the value of this parameter.
+ If True, then allow the input array `a` to be modified by intermediate
+ calculations, to save memory. In this case, the contents of the input
+ `a` after this function completes is undefined.
interpolation : {'linear', 'lower', 'higher', 'midpoint', 'nearest'}
This optional parameter specifies the interpolation method to
use when the desired quantile lies between two data points
``i < j``:
- * linear: ``i + (j - i) * fraction``, where ``fraction`` is
- the fractional part of the index surrounded by ``i`` and
- ``j``.
+ * linear: ``i + (j - i) * fraction``, where ``fraction``
+ is the fractional part of the index surrounded by ``i``
+ and ``j``.
* lower: ``i``.
* higher: ``j``.
* nearest: ``i`` or ``j``, whichever is nearest.
@@ -1097,7 +1091,9 @@ def nanpercentile(a, q, axis=None, out=None, overwrite_input=False,
See Also
--------
- nanmean, nanmedian, percentile, median, mean
+ nanmean
+ nanmedian : equivalent to ``nanpercentile(..., 50)``
+ percentile, median, mean
Notes
-----
@@ -1139,36 +1135,44 @@ def nanpercentile(a, q, axis=None, out=None, overwrite_input=False,
>>> assert not np.all(a==b)
"""
-
a = np.asanyarray(a)
- q = np.asanyarray(q)
+ q = np.true_divide(q, 100.0) # handles the asarray for us too
+ if not function_base._quantile_is_valid(q):
+ raise ValueError("Percentiles must be in the range [0, 100]")
+ return _nanquantile_unchecked(
+ a, q, axis, out, overwrite_input, interpolation, keepdims)
+
+
+def _nanquantile_unchecked(a, q, axis=None, out=None, overwrite_input=False,
+ interpolation='linear', keepdims=np._NoValue):
+ """Assumes that q is in [0, 1], and is an ndarray"""
# apply_along_axis in _nanpercentile doesn't handle empty arrays well,
# so deal them upfront
if a.size == 0:
return np.nanmean(a, axis, out=out, keepdims=keepdims)
- r, k = _ureduce(a, func=_nanpercentile, q=q, axis=axis, out=out,
- overwrite_input=overwrite_input,
- interpolation=interpolation)
+ r, k = function_base._ureduce(
+ a, func=_nanquantile_ureduce_func, q=q, axis=axis, out=out,
+ overwrite_input=overwrite_input, interpolation=interpolation
+ )
if keepdims and keepdims is not np._NoValue:
return r.reshape(q.shape + k)
else:
return r
-def _nanpercentile(a, q, axis=None, out=None, overwrite_input=False,
- interpolation='linear'):
+def _nanquantile_ureduce_func(a, q, axis=None, out=None, overwrite_input=False,
+ interpolation='linear'):
"""
Private function that doesn't support extended axis or keepdims.
These methods are extended to this function using _ureduce
See nanpercentile for parameter usage
-
"""
if axis is None or a.ndim == 1:
part = a.ravel()
- result = _nanpercentile1d(part, q, overwrite_input, interpolation)
+ result = _nanquantile_1d(part, q, overwrite_input, interpolation)
else:
- result = np.apply_along_axis(_nanpercentile1d, axis, a, q,
+ result = np.apply_along_axis(_nanquantile_1d, axis, a, q,
overwrite_input, interpolation)
# apply_along_axis fills in collapsed axis with results.
# Move that axis to the beginning to match percentile's
@@ -1181,9 +1185,9 @@ def _nanpercentile(a, q, axis=None, out=None, overwrite_input=False,
return result
-def _nanpercentile1d(arr1d, q, overwrite_input=False, interpolation='linear'):
+def _nanquantile_1d(arr1d, q, overwrite_input=False, interpolation='linear'):
"""
- Private function for rank 1 arrays. Compute percentile ignoring NaNs.
+ Private function for rank 1 arrays. Compute quantile ignoring NaNs.
See nanpercentile for parameter usage
"""
arr1d, overwrite_input = _remove_nan_1d(arr1d,
@@ -1191,8 +1195,8 @@ def _nanpercentile1d(arr1d, q, overwrite_input=False, interpolation='linear'):
if arr1d.size == 0:
return np.full(q.shape, np.nan)[()] # convert to scalar
- return np.percentile(arr1d, q, overwrite_input=overwrite_input,
- interpolation=interpolation)
+ return function_base._quantile_unchecked(
+ arr1d, q, overwrite_input=overwrite_input, interpolation=interpolation)
def nanvar(a, axis=None, dtype=None, out=None, ddof=0, keepdims=np._NoValue):