title: Sum of duration between each rows with regards to 2 field in PYTHON
body: <p>I have a set of datas in terms of timestamp, model and mode
  The mode comes in 4 different mode denote as (0,2,4,8)</p>
  
  <pre><code>Index  Model Timestamp              Mode
  1      x  2016-06-26 09:51:24.279   0
  2      x  2016-06-26 09:51:26.282   0
  3      x  2016-06-26 09:51:28.279   0
  4      x  2016-06-26 09:51:30.279   0
  5      y  2016-06-26 09:51:32.279   2
  6      y  2016-06-26 09:51:34.279   2
  7      x  2016-06-26 09:51:36.278   2
  8      x  2016-06-26 09:51:38.279   0
  9      x  2016-06-26 09:51:40.279   0
  10     y  2016-06-26 09:51:42.280   0
  11     x  2016-06-26 09:51:44.279   2
  12     x  2016-06-26 09:51:46.279   0
  13     x  2016-06-26 09:51:48.279   2
  14     x  2016-06-26 09:51:50.281   0
  15     y  2016-06-26 09:51:52.279   2
  16     y  2016-06-26 09:51:54.279   2
  17     x  2016-06-26 09:51:56.281   0
  18     x  2016-06-26 09:51:58.279   0
  19     x  2016-06-26 09:52:00.279   0
  </code></pre>
  
  <p>I will want to look for the time difference in their mode(mapped to the model). So if it transits from mode 0 to 2, at the 1st mode 2 should calculate 0, it should calculate the duration difference as shown below</p>
  
  <pre><code>Index  Model Timestamp              Mode  Diff
  1      x  2016-06-26 09:51:24.279   0     0
  2      x  2016-06-26 09:51:26.282   0     2.002
  3      x  2016-06-26 09:51:28.279   0     1.998
  4      x  2016-06-26 09:51:30.279   0     2
  5      y  2016-06-26 09:51:32.279   2     0
  6      y  2016-06-26 09:51:34.279   2     2
  7      x  2016-06-26 09:51:36.278   2     0
  8      x  2016-06-26 09:51:38.279   0     0
  9      x  2016-06-26 09:51:40.279   0     2
  10     y  2016-06-26 09:51:42.280   0     0
  11     x  2016-06-26 09:51:44.279   2     0
  12     x  2016-06-26 09:51:46.279   0     0
  13     x  2016-06-26 09:51:48.279   2     0
  14     x  2016-06-26 09:51:50.281   0     0
  15     y  2016-06-26 09:51:52.279   2     0
  16     y  2016-06-26 09:51:54.279   2     2
  17     x  2016-06-26 09:51:56.281   0     6
  18     x  2016-06-26 09:51:58.279   0     1.998
  19     x  2016-06-26 09:52:00.279   0     2
  </code></pre>
  
  <p>As of now, I do a sort (group time and mode) in mode then find the difference which is correct. I am unable to get the 0 when it transits. </p>
  
  <p>Please advice!</p>
  
  <pre><code>tdm = mydataset.groupby(["Model","Drive Mode"])["timestamp"]
  mydataset["DM-Diff"] = tdm.diff().fillna(0) / pd.datetools.timedelta(seconds = 1)
  mydataset = mydataset[mydataset["DM-Diff"] &lt; 180]
  mydataset = mydataset[mydataset["DM-Diff"] &gt; -1]#hardcoding with range so that it will not include other mode value
  </code></pre>