In [1]:
import numpy as np

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


Out[2]:
array([0, 0, 0, 1])

In [3]:
a.sum()


Out[3]:
1

In [4]:
a.mean()


Out[4]:
0.25

In [5]:
b = np.array([0, 0, np.nan, 1])
b


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

In [6]:
b.sum()


Out[6]:
nan

In [7]:
b.mean()


Out[7]:
nan

In [8]:
def sum_(x):
    return sum(y for y in x if not np.isnan(y))

In [9]:
sum_(a)


Out[9]:
1

In [10]:
sum_(b)


Out[10]:
1.0

In [11]:
def mean_(x):
    return np.array(list(y for y in x if not np.isnan(y))).mean()

In [12]:
mean_(a)


Out[12]:
0.25

In [13]:
mean_(b)


Out[13]:
0.33333333333333331

In [14]:
1. + np.nan


Out[14]:
nan

In [15]:
1. + 2. + 3.


Out[15]:
6.0

In [16]:
1. is np.nan


Out[16]:
False

In [17]:
np.nan is np.nan


Out[17]:
True

In [18]:
b[2]


Out[18]:
nan

In [19]:
b[2] is np.nan


Out[19]:
False

In [20]:
nan


---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-20-8496edc65712> in <module>()
----> 1 nan

NameError: name 'nan' is not defined

In [21]:
np.isnan(1.)


Out[21]:
False

In [22]:
np.isnan(np.nan)


Out[22]:
True

In [23]:
np.isnan(b[2])


Out[23]:
True

In [24]:
np.isnotnan(1.)


---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-24-96c92b9076a4> in <module>()
----> 1 np.isnotnan(1.)

AttributeError: module 'numpy' has no attribute 'isnotnan'

In [25]:
c = np.array([0, 0, None, 1])
c


Out[25]:
array([0, 0, None, 1], dtype=object)

In [26]:
c.sum()


---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-26-009f6e31c6cb> in <module>()
----> 1 c.sum()

/home/jep/anaconda3/envs/jupy/lib/python3.6/site-packages/numpy/core/_methods.py in _sum(a, axis, dtype, out, keepdims)
     30 
     31 def _sum(a, axis=None, dtype=None, out=None, keepdims=False):
---> 32     return umr_sum(a, axis, dtype, out, keepdims)
     33 
     34 def _prod(a, axis=None, dtype=None, out=None, keepdims=False):

TypeError: unsupported operand type(s) for +: 'int' and 'NoneType'

In [27]:
c.mean()


---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-27-5a30177bb035> in <module>()
----> 1 c.mean()

/home/jep/anaconda3/envs/jupy/lib/python3.6/site-packages/numpy/core/_methods.py in _mean(a, axis, dtype, out, keepdims)
     68             is_float16_result = True
     69 
---> 70     ret = umr_sum(arr, axis, dtype, out, keepdims)
     71     if isinstance(ret, mu.ndarray):
     72         ret = um.true_divide(

TypeError: unsupported operand type(s) for +: 'int' and 'NoneType'

In [28]:
d = np.array([0, 0, np.nan, 'np.nan', 1])
d


Out[28]:
array(['0', '0', 'nan', 'np.nan', '1'], 
      dtype='<U32')

In [29]:
for x in d:
    try:
        is_nan = np.isnan(x)
    except TypeError:
        is_nan = 'mu'
    print(type(x), is_nan, x)


<class 'numpy.str_'> mu 0
<class 'numpy.str_'> mu 0
<class 'numpy.str_'> mu nan
<class 'numpy.str_'> mu np.nan
<class 'numpy.str_'> mu 1

In [30]:
d = np.array([0, 0, np.nan, 1])
d


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

In [31]:
for x in d:
    try:
        is_nan = np.isnan(x)
    except TypeError:
        is_nan = 'mu'
    print(type(x), is_nan, x)


<class 'numpy.float64'> False 0.0
<class 'numpy.float64'> False 0.0
<class 'numpy.float64'> True nan
<class 'numpy.float64'> False 1.0