title: NumPy: calculate averages with NaNs removed
body: <p>How can I calculate matrix mean values along a matrix, but to remove <code>nan</code> values from calculation? (For R people, think <code>na.rm = TRUE</code>).</p>
  
  <p>Here is my [non-]working example:</p>
  
  <pre><code>import numpy as np
  dat = np.array([[1, 2, 3],
                  [4, 5, np.nan],
                  [np.nan, 6, np.nan],
                  [np.nan, np.nan, np.nan]])
  print(dat)
  print(dat.mean(1))  # [  2.  nan  nan  nan]
  </code></pre>
  
  <p>With NaNs removed, my expected output would be:</p>
  
  <pre><code>array([ 2.,  4.5,  6.,  nan])
  </code></pre>