title: pandas - how to resample respecting the first date?
body: <p>I'm struggling to find a proper way to solve an issue with resampling using pandas, instead of using ugly hacks that could be error prone.</p>
  
  <p>Suppose I have a series like this:</p>
  
  <pre><code>dates = pd.date_range('2018-01-03', '2018-09-13')
  s = pd.Series(range(len(dates)), index=dates)
  </code></pre>
  
  <p>Now, I want to resample the series to months, but starting from the first date from the index, and not from the first day of the month. If I just resample to "MS", for example, the first date I get is 2018-01-01, not 2018-01-03; Of course, it's "month start", so that's what I should expect, but how can I resample monthly without having the samples being picked from the start of the month, but rather from the same month day, such that the resulting index would be like ["2018-01-03", "2018-02-03", etc]?</p>
  
  <p>In other words, how can I resample with something like "month starting from first datetime in the index"?</p>
  
  <p>The only way I found so far, to do this, is to create some leading dates in the series, then do a negative shift, then using the loffset parameter (because neither shift and loffset respect the alignment of the data to the index), but this is a very ugly hack which I'd like to avoid.</p>
  
  <p>Thanks!</p>
  
  <p><strong>[SOLUTION]</strong></p>
  
  <p>I solved this by these simple steps, adapting from a number of attempts to fix this issue (including the first reply below, but also other sites and questions from SO):</p>
  
  <pre><code>dates = pd.date_range('2018-01-03', '2018-09-13')
  s = pd.Series(range(len(dates)), index=dates)
  delta = dates.min() - (dates.min() - offsets.MonthBegin(1))
  new_dates = dates - delta
  s2 = s.copy()
  s2.index = new_dates
  resampled = s2.resample('MS', loffset=delta)
  r_max = resampled.max()
  r_min = resampled.min()
  r_mean = resampled.mean()
  r_sum = resampled.sum()
  
  print('s\n', s)
  print('r_max\n', r_max)
  print('r_min\n', r_min)
  print('r_mean\n', r_mean)
  print('r_sum\n', r_sum)
  </code></pre>
  
  <p>Thanks for the help!</p>
  
  <p><strong>[EDIT] Alternative solution</strong></p>
  
  <p>I ended up taking a bit of a different approach at this. I start with something like:</p>
  
  <pre><code>dates = pd.date_range('2015-01-31', '2018-09-13')
  start_date: dt.date = dates[0].date()
  end_date: dt.date = dates[-1].date()
  recurrent_dates = []
  next_date = start_date
  while next_date &lt;= end_date:
      if next_date.day &lt; start_date.day:
          try:
              next_date = dt.date(
                  next_date.year, next_date.month, start_date.day)
          except ValueError:
              next_date = (
                      next_date + offsets.MonthBegin(1) - offsets.Day(1)
              ).date()
      recurrent_dates.append(next_date)
      next_date += relativedelta(months=1)
  </code></pre>
  
  <p>To have expected monthly periods starting at arbitrary dates, then create a DataFrame with the recurrent dates above, then reindex it with the dates from the original DataFrame with fillna(method="ffill"), then use groupby to group by the repeated dates.</p>
  
  <p>Hope this helps other people too.</p>