title: Pandas DataFrame.resample monthly offset from particular day of month
body: <p>I have a DataFrame <code>df</code> with sporadic daily business day rows (i.e., there is not always a row for every business day.)</p>
  <p>For each row in <code>df</code> I want to create a historical resampled mean <code>dfm</code> going back one month at a time.  For example, if I have a row for <code>2018-02-22</code> then I want rolling means for rows in the following date ranges:</p>
  <ul>
  <li><code>2018-01-23 : 2018-02-22</code></li>
  <li><code>2017-12-23 : 2018-01-22</code></li>
  <li><code>2017-11-23 : 2017-12-22</code></li>
  <li>etc.</li>
  </ul>
  <p>But I can't see a way to keep this pegged to the particular day of the month using <a href="http://pandas.pydata.org/pandas-docs/stable/timeseries.html#offset-aliases" rel="nofollow noreferrer">conventional offsets</a>.  For example, if I do:</p>
  <pre><code>dfm = df.resample('30D').mean()
  </code></pre>
  <p>Then we see two problems:</p>
  <ol>
  <li><p>It references the beginning of the DataFrame.  In fact, I can't find a way to force <code>.resample()</code> to peg itself to the end of the DataFrame – even if I have it operate on <code>df_reversed = df.loc[:'2018-02-22'].iloc[::-1]</code>.  Is there a way to &quot;peg&quot; the resampling to something other than the earliest date in the DataFrame?  (And ideally pegged to each particular row as I run some lambda on the associated historical resampling from each row's date?)</p>
  </li>
  <li><p>It will drift over time, because not every month is 30 days long.  So as I go back in time I will find that the interval 12 &quot;months&quot; prior ends <code>2017-02-27</code>, not <code>2017-02-22</code> like I want.</p>
  </li>
  </ol>
  <p>Knowing that I want to resample by non-overlapping &quot;months,&quot; the second problem can be well-defined for month days 29-31: For example, if I ask to resample for '2018-03-31' then the date ranges would end at the end of each preceding month:</p>
  <ul>
  <li><code>2018-03-01 : 2018-03-31</code></li>
  <li><code>2018-02-01 : 2018-02-28</code></li>
  <li><code>2018-01-01 : 2018-02-31</code></li>
  <li>etc.</li>
  </ul>
  <p>Though again, I don't know: is there a good or easy way to do this in pandas?</p>
  <hr />
  <h3>tl;dr:</h3>
  <p>Given something like the following:</p>
  <pre><code>someperiods = 20 # this can be a number of days covering many years
  somefrequency = '8D' # this can vary from 1D to maybe 10D
  rng = pd.date_range('2017-01-03', periods=someperiods, freq=somefrequency)
  df = pd.DataFrame({'x': rng.day}, index=rng) # x in practice is exogenous data
  
  from pandas.tseries.offsets import *
  df['MonthPrior'] = df.index.to_pydatetime() + DateOffset(months=-1)
  </code></pre>
  <p>Now:</p>
  <p>For each row in <code>df</code>: calculate <code>df['PreviousMonthMean']</code> = rolling average of all <code>df.x</code> in range <code>[df.MonthPrior, df.index)</code>.  In this example the resulting DataFrame would be:</p>
  <pre><code>Index       x   MonthPrior  PreviousMonthMean
  2017-01-03  3   2016-12-03  NaN
  2017-01-11  11  2016-12-11  3
  2017-01-19  19  2016-12-19  7
  2017-01-27  27  2016-12-27  11
  2017-02-04  4   2017-01-04  19
  2017-02-12  12  2017-01-12  16.66666667
  2017-02-20  20  2017-01-20  14.33333333
  2017-02-28  28  2017-01-28  12
  2017-03-08  8   2017-02-08  20
  2017-03-16  16  2017-02-16  18.66666667
  2017-03-24  24  2017-02-24  17.33333333
  2017-04-01  1   2017-03-01  16
  2017-04-09  9   2017-03-09  13.66666667
  2017-04-17  17  2017-03-17  11.33333333
  2017-04-25  25  2017-03-25  9
  2017-05-03  3   2017-04-03  17
  2017-05-11  11  2017-04-11  15
  2017-05-19  19  2017-04-19  13
  2017-05-27  27  2017-04-27  11
  2017-06-04  4   2017-05-04  19
  </code></pre>
  <p>If we can get that far, then I need to find an efficient way to iterate that so that for each row in <code>df</code> I can aggregate consecutive but <em>non-overlapping</em> <code>df['PreviousMonthMean']</code> values going back one calendar month at a time from the given DateTimeIndex....</p>