title: Pandas group by then count & sum based on date range +/- x-days
body: <p>I want to get a count &amp; sum of values over +/- 7 days period of a column after the dataframe being grouped to certain column</p>
  
  <p>Example data (edited to reflect my real dataset):</p>
  
  <pre><code>group  |        date          |   amount
  -------------------------------------------
  A      |  2017-12-26 04:20:20 |    50000.0
  A      |  2018-01-17 00:54:15 |    60000.0
  A      |  2018-01-27 06:10:12 |   150000.0
  A      |  2018-02-01 01:15:06 |   100000.0
  A      |  2018-02-11 05:05:34 |   150000.0
  A      |  2018-03-01 11:20:04 |   150000.0
  A      |  2018-03-16 12:14:01 |   150000.0
  A      |  2018-03-23 05:15:07 |   150000.0
  A      |  2018-04-02 10:40:35 |   150000.0
  </code></pre>
  
  <p>group by <code>group</code> then sum based on <code>date-7</code> &lt; <code>date</code> &lt; <code>date+7</code></p>
  
  <p>Results that I want:</p>
  
  <pre><code>group  |        date          |   amount    |  grouped_sum
  -----------------------------------------------------------
  A      |  2017-12-26 04:00:00 |    50000.0  |    50000.0
  A      |  2018-01-17 00:00:00 |    60000.0  |    60000.0
  A      |  2018-01-27 06:00:00 |   150000.0  |   250000.0
  A      |  2018-02-01 01:00:00 |   100000.0  |   250000.0
  A      |  2018-02-11 05:05:00 |   150000.0  |   150000.0
  A      |  2018-03-01 11:00:04 |   150000.0  |   150000.0
  A      |  2018-03-16 12:00:01 |   150000.0  |   150000.0
  A      |  2018-03-23 05:00:07 |   100000.0  |   100000.0
  A      |  2018-04-02 10:00:00 |   100000.0  |   100000.0
  </code></pre>
  
  <p>Quick snippet to achieve the dataset:</p>
  
  <pre><code>group = 9 * ['A']
  date = pd.to_datetime(['2017-12-26 04:20:20', '2018-01-17 00:54:15', 
                         '2018-01-27 06:10:12', '2018-02-01 01:15:06', 
                         '2018-02-11 05:05:34', '2018-03-01 11:20:04', 
                         '2018-03-16 12:14:01', '2018-03-23 05:15:07', 
                         '2018-04-02 10:40:35'])
  amount = [50000.0, 60000.0, 150000.0, 100000.0, 150000.0, 
            150000.0, 150000.0, 150000.0, 150000.0]
  df = pd.DataFrame({'group':group, 'date':date, 'amount':amount})
  </code></pre>
  
  <p>Bit of explanation:</p>
  
  <ul>
  <li>2nd row is 40 because it sums data for A in period 2018-01-14 and 2018-01-15</li>
  <li>4th row is 30 because it sums data for B in period 2018-01-03 + next 7 days</li>
  <li>6th row is 30 because it sums data for B in period 2018-01-03 + prev 7 days.</li>
  </ul>
  
  <p>I dont have any idea how to do sum over a period of date range. I might be able to do it if I make this way:</p>
  
  <p>1.Create another column that shows date-7 and date+7 for each rows</p>
  
  <pre><code>group  |    date     |  amount  |    date-7    |    date+7 
  -------------------------------------------------------------
  A      |  2017-12-26 |  50000.0 |  2017-12-19  |  2018-01-02
  A      |  2018-01-17 |  60000.0 |  2018-01-10  |  2018-01-24
  </code></pre>
  
  <p>2.calculate amount between the date range: <code>df[df.group == 'A' &amp; df.date &gt; df.date-7 &amp; df.date &lt; df.date+7].amount.sum()</code></p>
  
  <p>3.But this method is quite tedious.</p>
  
  <p>EDIT (2018-09-01):
  Found out this method below based on @jezrael answer which works for me but only works for single group:</p>
  
  <pre><code>t = pd.Timedelta(7, unit='d')
  def g(row):
      res = df[(df.created &gt; row.created - t) &amp; (df.created &lt; row.created + t)].amount.sum()
      return res
  
  df['new'] = df.apply(g, axis=1)
  </code></pre>