title: Subtracting values across grouped data frames in Pandas
body: <p>I have a set of IDs and Timestamps, and want to calculate the "total time elapsed per ID" by getting the difference of the oldest / earliest timestamps, grouped by ID.</p>
  
  <p>Data</p>
  
  <pre><code>id   timestamp
  1    2018-02-01 03:00:00
  1    2018-02-01 03:01:00
  2    2018-02-02 10:03:00
  2    2018-02-02 10:04:00
  2    2018-02-02 11:05:00
  </code></pre>
  
  <p>Expected Result</p>
  
  <p>(<em>I want the delta converted to minutes</em>)</p>
  
  <pre><code>id   delta
  1    1
  2    62
  </code></pre>
  
  <p>I have a for loop, but it's very slow (10+ min for 1M+ rows). I was wondering if this was achievable via pandas functions?</p>
  
  <pre><code># gb returns a DataFrameGroupedBy object, grouped by ID
  gb = df.groupby(['id'])
  
  # Create the resulting df
  cycletime = pd.DataFrame(columns=['id','timeDeltaMin'])
  
  def calculate_delta():
      for id, groupdf in gb:
          time = groupdf.timestamp
          # returns timestamp rows for the current id
  
          time_delta = time.max() - time.min()
  
          # convert Timedelta object to minutes
          time_delta = time_delta / pd.Timedelta(minutes=1) 
  
          # insert result to cycletime df
          cycletime.loc[-1] = [id,time_delta]
          cycletime.index += 1
  </code></pre>
  
  <p>Thinking of trying next:<br>
  - Multiprocessing</p>