Is this an efficient way to compute a moving average?
<p>I've some <code>{open|high|low|close}</code> market data. I want to compute a Simple Moving Average from the <code>close</code> value of each row.</p>

<p>I've had a look around and couldn't find a simple way to do this. I've computed it via the below method. I want to know if there is a better way:</p>

<pre><code>data = get_data_period_symbol('1h', 'EURUSD')

empty_list = np.zeros(len(data))

data['SMA10'] = empty_list
ma = 10

for i in range(ma-1, len(data)):
    vals = data['&lt;CLOSE&gt;'][i-(ma-1):i+1].tolist()
    mean = np.average(vals)
    index = data.index[i]
    data.set_value(index, 'SMA10', mean)
</code></pre>

<p>You can use <a href="http://docs.scipy.org/doc/numpy/reference/generated/numpy.convolve.html" rel="nofollow"><code>np.convolve</code></a> as suggested in this <a href="http://stackoverflow.com/a/22621523/6614295">answer</a>. So something like this should work:</p>

<pre><code>data.loc[ma-1:, "SMA10"] = np.convolve(data["&lt;CLOSE&gt;"], np.ones((ma,))/ma, mode="valid")
</code></pre>

<p>PS:
I just saw your own answer, which is actually a much nicer solution!</p>

python pandas dataframe moving-average