In [1]:
import numpy as np
import math
import sys

In [2]:
sys.version


Out[2]:
'3.6.0 |Continuum Analytics, Inc.| (default, Dec 23 2016, 12:22:00) \n[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)]'

In [3]:
nan1 = float('nan')
nan2 = math.nan
nan3 = np.nan
nan1, nan2, nan3


Out[3]:
(nan, nan, nan)

In [4]:
list(map(id, (nan1, nan2, nan3)))


Out[4]:
[140165328123608, 140165784020000, 140165359748248]

In [5]:
nan1 == nan1


Out[5]:
False

In [6]:
a = np.array([0., 0., 0., 1.])
a


Out[6]:
array([ 0.,  0.,  0.,  1.])

In [7]:
b = np.array([0., 0., float('nan'), 1.])
b


Out[7]:
array([  0.,   0.,  nan,   1.])

In [8]:
c = np.array([float('nan'), float('nan'), float('nan'), float('nan'), ])
c


Out[8]:
array([ nan,  nan,  nan,  nan])

In [9]:
[name for name in dir(a) if 'nan' in name.lower()]


Out[9]:
[]

In [10]:
for s in ('nan', 'mean', 'tile'):
    for name in dir(np):
        if s in name.lower():
            print(name)
    print()


NAN
NaN
isnan
nan
nan_to_num
nanargmax
nanargmin
nancumprod
nancumsum
nanmax
nanmean
nanmedian
nanmin
nanpercentile
nanprod
nanstd
nansum
nanvar

mean
nanmean

nanpercentile
percentile
tile


In [11]:
np.NAN is np.NaN, np.NAN is np.nan


Out[11]:
(True, True)

In [12]:
np.mean(a), np.nanmean(a)


Out[12]:
(0.25, 0.25)

In [13]:
np.mean(b), np.nanmean(b)


Out[13]:
(nan, 0.33333333333333331)

In [14]:
np.mean(c)


Out[14]:
nan

In [15]:
np.nanmean(c)


/home/jep/anaconda3/envs/jupy/lib/python3.6/site-packages/ipykernel/__main__.py:1: RuntimeWarning: Mean of empty slice
  if __name__ == '__main__':
Out[15]:
nan

In [16]:
help(np.percentile)


Help on function percentile in module numpy.lib.function_base:

percentile(a, q, axis=None, out=None, overwrite_input=False, interpolation='linear', keepdims=False)
    Compute the qth percentile of the data along the specified axis.
    
    Returns the qth percentile(s) of the array elements.
    
    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
        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.
    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`
        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.
    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``.
            * lower: ``i``.
            * higher: ``j``.
            * nearest: ``i`` or ``j``, whichever is nearest.
            * midpoint: ``(i + j) / 2``.
    
        .. versionadded:: 1.9.0
    keepdims : bool, optional
        If this is set to True, the axes which are reduced are left in
        the result as dimensions with size one. With this option, the
        result will broadcast correctly against the original array `a`.
    
        .. versionadded:: 1.9.0
    
    Returns
    -------
    percentile : scalar or ndarray
        If `q` is a single percentile and `axis=None`, then the result
        is a scalar. If multiple percentiles are given, first axis of
        the result corresponds to the percentiles. The other axes are
        the axes that remain after the reduction of `a`. If the input
        contains integers or floats smaller than ``float64``, the output
        data-type is ``float64``. Otherwise, the output data-type is the
        same as that of the input. If `out` is specified, that array is
        returned instead.
    
    See Also
    --------
    mean, median, nanpercentile
    
    Notes
    -----
    Given a vector ``V`` of length ``N``, the ``q``-th percentile of
    ``V`` is the value ``q/100`` of the way from the mimumum to the
    maximum in in a sorted copy of ``V``. The values and distances of
    the two nearest neighbors as well as the `interpolation` parameter
    will determine the percentile if the normalized ranking does not
    match the location of ``q`` exactly. This function is the same as
    the median if ``q=50``, the same as the minimum if ``q=0`` and the
    same as the maximum if ``q=100``.
    
    Examples
    --------
    >>> a = np.array([[10, 7, 4], [3, 2, 1]])
    >>> a
    array([[10,  7,  4],
           [ 3,  2,  1]])
    >>> np.percentile(a, 50)
    3.5
    >>> np.percentile(a, 50, axis=0)
    array([[ 6.5,  4.5,  2.5]])
    >>> np.percentile(a, 50, axis=1)
    array([ 7.,  2.])
    >>> np.percentile(a, 50, axis=1, keepdims=True)
    array([[ 7.],
           [ 2.]])
    
    >>> m = np.percentile(a, 50, axis=0)
    >>> out = np.zeros_like(m)
    >>> np.percentile(a, 50, axis=0, out=out)
    array([[ 6.5,  4.5,  2.5]])
    >>> m
    array([[ 6.5,  4.5,  2.5]])
    
    >>> b = a.copy()
    >>> np.percentile(b, 50, axis=1, overwrite_input=True)
    array([ 7.,  2.])
    >>> assert not np.all(a == b)


In [17]:
for x in (a, b):
    for f in (np.percentile, np.nanpercentile):
        print(f'for {f}({x})')
        y = f(x, q=10)
        print(y)


for <function percentile at 0x7f7ac898b510>([ 0.  0.  0.  1.])
0.0
for <function nanpercentile at 0x7f7ac899bd08>([ 0.  0.  0.  1.])
0.0
for <function percentile at 0x7f7ac898b510>([  0.   0.  nan   1.])
nan
for <function nanpercentile at 0x7f7ac899bd08>([  0.   0.  nan   1.])
0.0
/home/jep/anaconda3/envs/jupy/lib/python3.6/site-packages/numpy/lib/function_base.py:4116: RuntimeWarning: Invalid value encountered in percentile
  interpolation=interpolation)

In [18]:
q = 10

In [19]:
np.percentile(a, q)


Out[19]:
0.0

In [20]:
np.percentile(b, q)


/home/jep/anaconda3/envs/jupy/lib/python3.6/site-packages/numpy/lib/function_base.py:4116: RuntimeWarning: Invalid value encountered in percentile
  interpolation=interpolation)
Out[20]:
nan

In [21]:
np.percentile(c, q)


/home/jep/anaconda3/envs/jupy/lib/python3.6/site-packages/numpy/lib/function_base.py:4116: RuntimeWarning: Invalid value encountered in percentile
  interpolation=interpolation)
Out[21]:
nan

In [22]:
np.nanpercentile(a, q)


Out[22]:
0.0

In [23]:
np.nanpercentile(b, q)


Out[23]:
0.0

In [24]:
np.nanpercentile(c, q)


/home/jep/anaconda3/envs/jupy/lib/python3.6/site-packages/numpy/lib/function_base.py:3858: RuntimeWarning: All-NaN slice encountered
  r = func(a, **kwargs)
Out[24]:
nan

In [25]:
np.mean(a)


Out[25]:
0.25

In [26]:
np.mean(b)


Out[26]:
nan

In [27]:
np.mean(c)


Out[27]:
nan

In [28]:
np.nanmean(a)


Out[28]:
0.25

In [29]:
np.nanmean(b)


Out[29]:
0.33333333333333331

In [30]:
np.nanmean(c)


/home/jep/anaconda3/envs/jupy/lib/python3.6/site-packages/ipykernel/__main__.py:1: RuntimeWarning: Mean of empty slice
  if __name__ == '__main__':
Out[30]:
nan

In [31]:
np.sum(a)


Out[31]:
1.0

In [32]:
np.sum(b)


Out[32]:
nan

In [33]:
np.sum(c)


Out[33]:
nan

In [34]:
np.nansum(a)


Out[34]:
1.0

In [35]:
np.nansum(b)


Out[35]:
1.0

In [36]:
np.nansum(c)


Out[36]:
0.0