title: How to calculate time difference by group using pandas?
body: <h2>Problem</h2>
  
  <p>I want to calculate <code>diff</code> by group. And I don’t know how to sort the <code>time</code> column so that each group results are sorted and positive. </p>
  
  <p>The original data :</p>
  
  <pre><code>In [37]: df 
  Out[37]:
    id                time
  0  A 2016-11-25 16:32:17
  1  A 2016-11-25 16:36:04
  2  A 2016-11-25 16:35:29
  3  B 2016-11-25 16:35:24
  4  B 2016-11-25 16:35:46
  </code></pre>
  
  <p>The result I want </p>
  
  <pre><code>Out[40]:
     id   time
  0  A   00:35
  1  A   03:12
  2  B   00:22
  </code></pre>
  
  <p>notice: the type of time col is timedelta64[ns]</p>
  
  <h2>Trying</h2>
  
  <pre><code>In [38]: df['time'].diff(1)
  Out[38]:
  0                 NaT
  1            00:03:47
  2   -1 days +23:59:25
  3   -1 days +23:59:55
  4            00:00:22
  Name: time, dtype: timedelta64[ns]
  </code></pre>
  
  <p>Don't get desired result.</p>
  
  <h2>Hope</h2>
  
  <p>Not only solve the problem but the code can run fast because there are 50 million rows.</p>