title: Pandas count the number of times an event has occurred in last n days by group
body: <p>I have table of events occurring by id. How would I count the number of times in the last n days that each event type has occurred prior to the current row?</p>
  
  <p>For example with a list of events like:</p>
  
  <pre><code>df = pd.DataFrame([{'id': 1, 'event_day': '2016-01-01', 'event_type': 'type1'},
  {'id': 1, 'event_day': '2016-01-02', 'event_type': 'type1'},
  {'id': 2, 'event_day': '2016-02-01', 'event_type': 'type2'},
  {'id': 2, 'event_day': '2016-02-15', 'event_type': 'type3'},
  {'id': 3, 'event_day': '2016-01-06', 'event_type': 'type3'},
  {'id': 3, 'event_day': '2016-03-11', 'event_type': 'type3'},])
  df['event_day'] = pd.to_datetime(df['event_day'])
  df = df.sort_values(['id', 'event_day'])
  </code></pre>
  
  <p>or: </p>
  
  <pre><code>   event_day event_type  id
  0 2016-01-01      type1   1
  1 2016-01-02      type1   1
  2 2016-02-01      type2   2
  3 2016-02-15      type3   2
  4 2016-01-06      type3   3
  5 2016-03-11      type3   3
  </code></pre>
  
  <p>by <code>id</code> I want to count the number of times each <code>event_type</code> has occurred prior to the current row in the last n days. For example, in row 3 id=2, so how many times up to (but not including) that point in the event history have events types 1, 2, and 3 occurred in the last n days for id 2?</p>
  
  <p>The desired output would look something like below:</p>
  
  <pre><code>    event_day   event_type  event_type1_in_last_30days  event_type2_in_last_30days  event_type3_in_last_30days  id
  0   2016-01-01  type1       0                           0                           0                           1
  1   2016-01-02  type1       1                           0                           0                           1
  2   2016-02-01  type2       0                           0                           0                           2
  3   2016-02-15  type3       0                           1                           0                           2
  4   2016-01-06  type3       0                           0                           0                           3
  5   2016-03-11  type3       0                           0                           0                           3
  </code></pre>