<p dir="auto">For even numbered periods the centered moving average is calculated incorrectly. Suppose the period length is 5. Then the center of 5 periods is 3. However if the period length is 4 then the center of the period is 2.5. The value at index 3 should be the average of the values at 2.5 and 3.5. Pandas is showing the 2.5 value at 3 which is incorrect.</p>
<p dir="auto">EXAMPLE:<br>
a=pd.Series(range(1,6), index=range(1,6))<br>
a.rolling(4, center=True).mean()</p>
<p dir="auto">1    NaN<br>
2    NaN<br>
3    2.5<br>
4    3.5<br>
5    NaN</p>
<p dir="auto">Seems like there is a bug in the centered mooving window functions, although the examples <a href="http://pandas.pydata.org/pandas-docs/stable/computation.html#moving-rolling-statistics-moments" rel="nofollow">here</a> don't show it.<br>
Also <code class="notranslate">np.convolve</code> used like in <a href="http://stackoverflow.com/a/11352216/1301710" rel="nofollow">this post</a> on Stackoverflow shows different results for the first values:</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="In [109]: ser = pd.Series(randn(20), index=pd.date_range('1/1/2000', periods=20))

In [110]: df = pd.DataFrame(ser)

In [111]: df['rc'] = pd.rolling_count(ser, 10, center=True)

In [112]: df['rm'] = pd.rolling_mean(ser, 10, center=True)

In [113]: df['rm_min_1'] = pd.rolling_mean(ser, 10, center=True, min_periods=1)

In [114]: def movingaverage(interval, window_size):
   .....:         window = numpy.ones(int(window_size))/float(window_size)
   .....:         return numpy.convolve(interval, window, 'same')
   .....: 

In [116]: df['ma'] = movingaverage(ser, 10)

In [117]: df
Out[117]: 
                   0  rc        rm  rm_min_1        ma
2000-01-01  1.177692   5       NaN  0.048306  0.024153
2000-01-02 -0.068567   6       NaN -0.094287 -0.056572
2000-01-03  0.194942   7       NaN -0.225103 -0.157572
2000-01-04 -0.179778   8       NaN -0.160040 -0.128032
2000-01-05 -0.882760   9       NaN -0.262176 -0.235959
2000-01-06 -0.807252  10 -0.250828 -0.250828 -0.250828
2000-01-07 -1.009999  10 -0.365703 -0.365703 -0.365703
2000-01-08  0.295402  10 -0.431110 -0.431110 -0.431110
2000-01-09 -1.079267  10 -0.408968 -0.408968 -0.408968
2000-01-10 -0.148689  10 -0.456946 -0.456946 -0.456946
2000-01-11  0.028935  10 -0.290453 -0.290453 -0.290453
2000-01-12 -0.722632  10 -0.204166 -0.204166 -0.204166
2000-01-13  0.416364  10 -0.175837 -0.175837 -0.175837
2000-01-14 -0.659560  10 -0.266773 -0.266773 -0.266773
2000-01-15  0.782165  10 -0.184003 -0.184003 -0.184003
2000-01-16  0.055617  10 -0.338359 -0.338359 -0.338359
2000-01-17 -0.726706   0       NaN       NaN -0.341252
2000-01-18 -0.613954   0       NaN       NaN -0.268989
2000-01-19 -0.251574   0       NaN       NaN -0.310625
2000-01-20 -1.692243   0       NaN       NaN -0.244669"><pre class="notranslate"><span class="pl-v">In</span> [<span class="pl-c1">109</span>]: <span class="pl-s1">ser</span> <span class="pl-c1">=</span> <span class="pl-s1">pd</span>.<span class="pl-v">Series</span>(<span class="pl-en">randn</span>(<span class="pl-c1">20</span>), <span class="pl-s1">index</span><span class="pl-c1">=</span><span class="pl-s1">pd</span>.<span class="pl-en">date_range</span>(<span class="pl-s">'1/1/2000'</span>, <span class="pl-s1">periods</span><span class="pl-c1">=</span><span class="pl-c1">20</span>))

<span class="pl-v">In</span> [<span class="pl-c1">110</span>]: <span class="pl-s1">df</span> <span class="pl-c1">=</span> <span class="pl-s1">pd</span>.<span class="pl-v">DataFrame</span>(<span class="pl-s1">ser</span>)

<span class="pl-v">In</span> [<span class="pl-c1">111</span>]: <span class="pl-s1">df</span>[<span class="pl-s">'rc'</span>] <span class="pl-c1">=</span> <span class="pl-s1">pd</span>.<span class="pl-en">rolling_count</span>(<span class="pl-s1">ser</span>, <span class="pl-c1">10</span>, <span class="pl-s1">center</span><span class="pl-c1">=</span><span class="pl-c1">True</span>)

<span class="pl-v">In</span> [<span class="pl-c1">112</span>]: <span class="pl-s1">df</span>[<span class="pl-s">'rm'</span>] <span class="pl-c1">=</span> <span class="pl-s1">pd</span>.<span class="pl-en">rolling_mean</span>(<span class="pl-s1">ser</span>, <span class="pl-c1">10</span>, <span class="pl-s1">center</span><span class="pl-c1">=</span><span class="pl-c1">True</span>)

<span class="pl-v">In</span> [<span class="pl-c1">113</span>]: <span class="pl-s1">df</span>[<span class="pl-s">'rm_min_1'</span>] <span class="pl-c1">=</span> <span class="pl-s1">pd</span>.<span class="pl-en">rolling_mean</span>(<span class="pl-s1">ser</span>, <span class="pl-c1">10</span>, <span class="pl-s1">center</span><span class="pl-c1">=</span><span class="pl-c1">True</span>, <span class="pl-s1">min_periods</span><span class="pl-c1">=</span><span class="pl-c1">1</span>)

<span class="pl-v">In</span> [<span class="pl-c1">114</span>]: <span class="pl-s1">def</span> <span class="pl-en">movingaverage</span>(<span class="pl-s1">interval</span>, <span class="pl-s1">window_size</span>):
   .....:         <span class="pl-s1">window</span> <span class="pl-c1">=</span> <span class="pl-s1">numpy</span>.<span class="pl-en">ones</span>(<span class="pl-en">int</span>(<span class="pl-s1">window_size</span>))<span class="pl-c1">/</span><span class="pl-en">float</span>(<span class="pl-s1">window_size</span>)
   .....:         <span class="pl-k">return</span> <span class="pl-s1">numpy</span>.<span class="pl-en">convolve</span>(<span class="pl-s1">interval</span>, <span class="pl-s1">window</span>, <span class="pl-s">'same'</span>)
   .....: 

<span class="pl-v">In</span> [<span class="pl-c1">116</span>]: <span class="pl-s1">df</span>[<span class="pl-s">'ma'</span>] <span class="pl-c1">=</span> <span class="pl-en">movingaverage</span>(<span class="pl-s1">ser</span>, <span class="pl-c1">10</span>)

<span class="pl-v">In</span> [<span class="pl-c1">117</span>]: <span class="pl-s1">df</span>
<span class="pl-v">Out</span>[<span class="pl-c1">117</span>]: 
                   <span class="pl-c1">0</span>  <span class="pl-s1">rc</span>        <span class="pl-s1">rm</span>  <span class="pl-s1">rm_min_1</span>        <span class="pl-s1">ma</span>
<span class="pl-c1">2000</span><span class="pl-c1">-</span><span class="pl-c1">01</span><span class="pl-c1">-</span><span class="pl-c1">01</span>  <span class="pl-c1">1.177692</span>   <span class="pl-c1">5</span>       <span class="pl-v">NaN</span>  <span class="pl-c1">0.048306</span>  <span class="pl-c1">0.024153</span>
<span class="pl-c1">2000</span><span class="pl-c1">-</span><span class="pl-c1">01</span><span class="pl-c1">-</span><span class="pl-c1">02</span> <span class="pl-c1">-</span><span class="pl-c1">0.068567</span>   <span class="pl-c1">6</span>       <span class="pl-v">NaN</span> <span class="pl-c1">-</span><span class="pl-c1">0.094287</span> <span class="pl-c1">-</span><span class="pl-c1">0.056572</span>
<span class="pl-c1">2000</span><span class="pl-c1">-</span><span class="pl-c1">01</span><span class="pl-c1">-</span><span class="pl-c1">03</span>  <span class="pl-c1">0.194942</span>   <span class="pl-c1">7</span>       <span class="pl-v">NaN</span> <span class="pl-c1">-</span><span class="pl-c1">0.225103</span> <span class="pl-c1">-</span><span class="pl-c1">0.157572</span>
<span class="pl-c1">2000</span><span class="pl-c1">-</span><span class="pl-c1">01</span><span class="pl-c1">-</span><span class="pl-c1">04</span> <span class="pl-c1">-</span><span class="pl-c1">0.179778</span>   <span class="pl-c1">8</span>       <span class="pl-v">NaN</span> <span class="pl-c1">-</span><span class="pl-c1">0.160040</span> <span class="pl-c1">-</span><span class="pl-c1">0.128032</span>
<span class="pl-c1">2000</span><span class="pl-c1">-</span><span class="pl-c1">01</span><span class="pl-c1">-</span><span class="pl-c1">05</span> <span class="pl-c1">-</span><span class="pl-c1">0.882760</span>   <span class="pl-c1">9</span>       <span class="pl-v">NaN</span> <span class="pl-c1">-</span><span class="pl-c1">0.262176</span> <span class="pl-c1">-</span><span class="pl-c1">0.235959</span>
<span class="pl-c1">2000</span><span class="pl-c1">-</span><span class="pl-c1">01</span><span class="pl-c1">-</span><span class="pl-c1">06</span> <span class="pl-c1">-</span><span class="pl-c1">0.807252</span>  <span class="pl-c1">10</span> <span class="pl-c1">-</span><span class="pl-c1">0.250828</span> <span class="pl-c1">-</span><span class="pl-c1">0.250828</span> <span class="pl-c1">-</span><span class="pl-c1">0.250828</span>
<span class="pl-c1">2000</span><span class="pl-c1">-</span><span class="pl-c1">01</span><span class="pl-c1">-</span><span class="pl-c1">07</span> <span class="pl-c1">-</span><span class="pl-c1">1.009999</span>  <span class="pl-c1">10</span> <span class="pl-c1">-</span><span class="pl-c1">0.365703</span> <span class="pl-c1">-</span><span class="pl-c1">0.365703</span> <span class="pl-c1">-</span><span class="pl-c1">0.365703</span>
<span class="pl-c1">2000</span><span class="pl-c1">-</span><span class="pl-c1">01</span><span class="pl-c1">-</span><span class="pl-c1">08</span>  <span class="pl-c1">0.295402</span>  <span class="pl-c1">10</span> <span class="pl-c1">-</span><span class="pl-c1">0.431110</span> <span class="pl-c1">-</span><span class="pl-c1">0.431110</span> <span class="pl-c1">-</span><span class="pl-c1">0.431110</span>
<span class="pl-c1">2000</span><span class="pl-c1">-</span><span class="pl-c1">01</span><span class="pl-c1">-</span><span class="pl-c1">09</span> <span class="pl-c1">-</span><span class="pl-c1">1.079267</span>  <span class="pl-c1">10</span> <span class="pl-c1">-</span><span class="pl-c1">0.408968</span> <span class="pl-c1">-</span><span class="pl-c1">0.408968</span> <span class="pl-c1">-</span><span class="pl-c1">0.408968</span>
<span class="pl-c1">2000</span><span class="pl-c1">-</span><span class="pl-c1">01</span><span class="pl-c1">-</span><span class="pl-c1">10</span> <span class="pl-c1">-</span><span class="pl-c1">0.148689</span>  <span class="pl-c1">10</span> <span class="pl-c1">-</span><span class="pl-c1">0.456946</span> <span class="pl-c1">-</span><span class="pl-c1">0.456946</span> <span class="pl-c1">-</span><span class="pl-c1">0.456946</span>
<span class="pl-c1">2000</span><span class="pl-c1">-</span><span class="pl-c1">01</span><span class="pl-c1">-</span><span class="pl-c1">11</span>  <span class="pl-c1">0.028935</span>  <span class="pl-c1">10</span> <span class="pl-c1">-</span><span class="pl-c1">0.290453</span> <span class="pl-c1">-</span><span class="pl-c1">0.290453</span> <span class="pl-c1">-</span><span class="pl-c1">0.290453</span>
<span class="pl-c1">2000</span><span class="pl-c1">-</span><span class="pl-c1">01</span><span class="pl-c1">-</span><span class="pl-c1">12</span> <span class="pl-c1">-</span><span class="pl-c1">0.722632</span>  <span class="pl-c1">10</span> <span class="pl-c1">-</span><span class="pl-c1">0.204166</span> <span class="pl-c1">-</span><span class="pl-c1">0.204166</span> <span class="pl-c1">-</span><span class="pl-c1">0.204166</span>
<span class="pl-c1">2000</span><span class="pl-c1">-</span><span class="pl-c1">01</span><span class="pl-c1">-</span><span class="pl-c1">13</span>  <span class="pl-c1">0.416364</span>  <span class="pl-c1">10</span> <span class="pl-c1">-</span><span class="pl-c1">0.175837</span> <span class="pl-c1">-</span><span class="pl-c1">0.175837</span> <span class="pl-c1">-</span><span class="pl-c1">0.175837</span>
<span class="pl-c1">2000</span><span class="pl-c1">-</span><span class="pl-c1">01</span><span class="pl-c1">-</span><span class="pl-c1">14</span> <span class="pl-c1">-</span><span class="pl-c1">0.659560</span>  <span class="pl-c1">10</span> <span class="pl-c1">-</span><span class="pl-c1">0.266773</span> <span class="pl-c1">-</span><span class="pl-c1">0.266773</span> <span class="pl-c1">-</span><span class="pl-c1">0.266773</span>
<span class="pl-c1">2000</span><span class="pl-c1">-</span><span class="pl-c1">01</span><span class="pl-c1">-</span><span class="pl-c1">15</span>  <span class="pl-c1">0.782165</span>  <span class="pl-c1">10</span> <span class="pl-c1">-</span><span class="pl-c1">0.184003</span> <span class="pl-c1">-</span><span class="pl-c1">0.184003</span> <span class="pl-c1">-</span><span class="pl-c1">0.184003</span>
<span class="pl-c1">2000</span><span class="pl-c1">-</span><span class="pl-c1">01</span><span class="pl-c1">-</span><span class="pl-c1">16</span>  <span class="pl-c1">0.055617</span>  <span class="pl-c1">10</span> <span class="pl-c1">-</span><span class="pl-c1">0.338359</span> <span class="pl-c1">-</span><span class="pl-c1">0.338359</span> <span class="pl-c1">-</span><span class="pl-c1">0.338359</span>
<span class="pl-c1">2000</span><span class="pl-c1">-</span><span class="pl-c1">01</span><span class="pl-c1">-</span><span class="pl-c1">17</span> <span class="pl-c1">-</span><span class="pl-c1">0.726706</span>   <span class="pl-c1">0</span>       <span class="pl-v">NaN</span>       <span class="pl-v">NaN</span> <span class="pl-c1">-</span><span class="pl-c1">0.341252</span>
<span class="pl-c1">2000</span><span class="pl-c1">-</span><span class="pl-c1">01</span><span class="pl-c1">-</span><span class="pl-c1">18</span> <span class="pl-c1">-</span><span class="pl-c1">0.613954</span>   <span class="pl-c1">0</span>       <span class="pl-v">NaN</span>       <span class="pl-v">NaN</span> <span class="pl-c1">-</span><span class="pl-c1">0.268989</span>
<span class="pl-c1">2000</span><span class="pl-c1">-</span><span class="pl-c1">01</span><span class="pl-c1">-</span><span class="pl-c1">19</span> <span class="pl-c1">-</span><span class="pl-c1">0.251574</span>   <span class="pl-c1">0</span>       <span class="pl-v">NaN</span>       <span class="pl-v">NaN</span> <span class="pl-c1">-</span><span class="pl-c1">0.310625</span>
<span class="pl-c1">2000</span><span class="pl-c1">-</span><span class="pl-c1">01</span><span class="pl-c1">-</span><span class="pl-c1">20</span> <span class="pl-c1">-</span><span class="pl-c1">1.692243</span>   <span class="pl-c1">0</span>       <span class="pl-v">NaN</span>       <span class="pl-v">NaN</span> <span class="pl-c1">-</span><span class="pl-c1">0.244669</span></pre></div>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="In [5]: import numpy

In [6]: In [20]: ser = pd.Series(range(10))
   ...:     ...: df = pd.DataFrame(ser)
   ...:     ...: N=3
   ...:     ...: df['rc'] = ser.rolling(N, center=True).count()
   ...:     ...: df['rm'] = ser.rolling(N, center=True).mean()
   ...:     ...: df['rm_min_1'] = ser.rolling(N, center=True, min_periods=1).mean()
   ...:     ...: def movingaverage(interval, window_size):
   ...:     ...:     window = numpy.ones(int(window_size))/float(window_size)
   ...:     ...:     return numpy.convolve(interval, window, 'same')
   ...:     ...:
   ...:     ...: df['ma'] = movingaverage(ser, N)

In [7]: df
Out[7]:
   0   rc   rm  rm_min_1        ma
0  0  2.0  NaN       0.5  0.333333
1  1  3.0  1.0       1.0  1.000000
2  2  3.0  2.0       2.0  2.000000
3  3  3.0  3.0       3.0  3.000000
4  4  3.0  4.0       4.0  4.000000
5  5  3.0  5.0       5.0  5.000000
6  6  3.0  6.0       6.0  6.000000
7  7  3.0  7.0       7.0  7.000000
8  8  3.0  8.0       8.0  8.000000
9  9  2.0  NaN       8.5  5.666667"><pre class="notranslate"><code class="notranslate">In [5]: import numpy

In [6]: In [20]: ser = pd.Series(range(10))
   ...:     ...: df = pd.DataFrame(ser)
   ...:     ...: N=3
   ...:     ...: df['rc'] = ser.rolling(N, center=True).count()
   ...:     ...: df['rm'] = ser.rolling(N, center=True).mean()
   ...:     ...: df['rm_min_1'] = ser.rolling(N, center=True, min_periods=1).mean()
   ...:     ...: def movingaverage(interval, window_size):
   ...:     ...:     window = numpy.ones(int(window_size))/float(window_size)
   ...:     ...:     return numpy.convolve(interval, window, 'same')
   ...:     ...:
   ...:     ...: df['ma'] = movingaverage(ser, N)

In [7]: df
Out[7]:
   0   rc   rm  rm_min_1        ma
0  0  2.0  NaN       0.5  0.333333
1  1  3.0  1.0       1.0  1.000000
2  2  3.0  2.0       2.0  2.000000
3  3  3.0  3.0       3.0  3.000000
4  4  3.0  4.0       4.0  4.000000
5  5  3.0  5.0       5.0  5.000000
6  6  3.0  6.0       6.0  6.000000
7  7  3.0  7.0       7.0  7.000000
8  8  3.0  8.0       8.0  8.000000
9  9  2.0  NaN       8.5  5.666667
</code></pre></div>