title: Rolling Average in Pandas
body: <p>I have a dataframe with 2 columns - Date and Price. The data is sorted with newest date first (23 Jan in first row, 22 Jan in second row and so on). </p>
  
  <pre><code>Date   Price
  23 Jan 100
  22 Jan 95
  21 Jan 90
  .
  .
  .
  </code></pre>
  
  <p>I want to calculate 2 days rolling average price for this time series data. I am using this:</p>
  
  <pre><code>df.rolling(2).mean()
  </code></pre>
  
  <p>What this does is, it assigns NaN to the first row (23 Jan) and then for the second row gives the output as the mean of prices on 23 Jan and 22 Jan. This is not useful as 22 Jan average is using forward data (price of 23 Jan). What I need is that the moving average value for 23 Jan is the average of 23 Jan &amp; 22 Jan. This way the last value of MA would be NaN instead of first value.</p>
  
  <p>What I do not want to do is sort this data with oldest first, compute and then resort.</p>
  
  <p>I had the same issue with pct_change(). However, pct_change(-1) solved that issue. But rolling does not accept negative value as an input. Please suggest a workaround this issue. Thanks.</p>