title: Pandas Rolling mean with GroupBy and Sort
body: <p>I have a DataFrame that looks like:</p>
  
  <pre><code>f_period f_year f_month subject month year value
  20140102 2014   1      a        1     2018 10
  20140109 2014   1      a        1     2018 12
  20140116 2014   1      a        1     2018 8
  20140202 2014   2      a        1     2018 20
  20140209 2014   2      a        1     2018 15
  20140102 2014   1      b        1     2018 10
  20140109 2014   1      b        1     2018 12
  20140116 2014   1      b        1     2018 8
  20140202 2014   2      b        1     2018 20
  20140209 2014   2      b        1     2018 15
  </code></pre>
  
  <p>The <code>f_period</code> is the date when a forecast for a SKU (column <code>subject</code>) was made. The <code>month</code> and <code>year</code> column is the period for which the forecast was made. For example, the first row says that on <code>01/02/2018</code>, the model was forecasting to set <code>10</code> units of product <code>a</code> in month <code>1</code> of year<code>2018</code>.</p>
  
  <p>I am trying to create a rolling average prediction by <code>subject</code>, by <code>month</code> for 2 <code>f_months</code>. The DataFrame should look like:</p>
  
  <pre><code>f_period f_year f_month subject month year value mnthly_avg rolling_2_avg
  20140102 2014   1      a        1     2018 10    10         13
  20140109 2014   1      a        1     2018 12    10         13
  20140116 2014   1      a        1     2018 8     10         13
  20140202 2014   2      a        1     2018 20    17.5       null
  20140209 2014   2      a        1     2018 15    17.5       null
  20140102 2014   1      b        1     2018 10    10         13
  20140109 2014   1      b        1     2018 12    10         13
  20140116 2014   1      b        1     2018 8     10         13
  20140202 2014   2      b        1     2018 20    17.5       null
  20140209 2014   2      b        1     2018 15    17.5       null
  </code></pre>
  
  <p><strong>Things I tried:</strong></p>
  
  <p>I was able to get <code>mnthly_avg</code> by :</p>
  
  <pre><code>data_df['monthly_avg'] = data_df.groupby(['f_month', 'f_year', 'year', 'month', 'period', 'subject']).\
          value.transform('mean')
  </code></pre>
  
  <p>I tried getting the <code>rolling_2_avg</code> :</p>
  
  <pre><code>rolling_monthly_df = data_df[['f_year', 'f_month', 'subject', 'month', 'year', 'value', 'f_period']].\
              groupby(['f_year', 'f_month', 'subject', 'month', 'year']).value.mean().reset_index()
  
  rolling_monthly_df['rolling_2_avg'] = rolling_monthly_df.groupby(['subject', 'month']).\
              value.rolling(2).mean().reset_index(drop=True)
  </code></pre>
  
  <p>This gave me an unexpected output. I don't understand how it calculated the values for <code>rolling_2_avg</code></p>
  
  <p>How do I group by <code>subject</code> and <code>month</code> and then sort by <code>f_month</code> and then take the average of the next two-month average? </p>