Calculate moving average in numpy array with NaNs
<p>I am trying to calculate the moving average in a large numpy array that contains NaNs. Currently I am using:</p>

<pre><code>import numpy as np

def moving_average(a,n=5):
      ret = np.cumsum(a,dtype=float)
      ret[n:] = ret[n:]-ret[:-n]
      return ret[-1:]/n
</code></pre>

<p>When calculating with a masked array:</p>

<pre><code>x = np.array([1.,3,np.nan,7,8,1,2,4,np.nan,np.nan,4,4,np.nan,1,3,6,3])
mx = np.ma.masked_array(x,np.isnan(x))
y = moving_average(mx).filled(np.nan)

print y

&gt;&gt;&gt; array([3.8,3.8,3.6,nan,nan,nan,2,2.4,nan,nan,nan,2.8,2.6])
</code></pre>

<p>The result I am looking for (below) should ideally have NaNs only in the place where the original array, x, had NaNs and the averaging should be done over the number of non-NaN elements in the grouping (I need some way to change the size of n in the function.)</p>

<pre><code>y = array([4.75,4.75,nan,4.4,3.75,2.33,3.33,4,nan,nan,3,3.5,nan,3.25,4,4.5,3])
</code></pre>

<p>I could loop over the entire array and check index by index but the array I am using is very large and that would take a long time. Is there a numpythonic way to do this? </p>

<p>You could create a temporary array and use np.nanmean() (new in version 1.8 if I'm not mistaken):</p>

<pre><code>import numpy as np
temp = np.vstack([x[i:-(5-i)] for i in range(5)]) # stacks vertically the strided arrays
means = np.nanmean(temp, axis=0)
</code></pre>

<p>and put original nan back in place with <code>means[np.isnan(x[:-5])] = np.nan</code></p>

<p>However this look redundant both in terms of memory (stacking the same array strided 5 times) and computation.</p>

python numpy masked-array