title: Determine change in values in a grouped dataframe
body: <p>Assume a dataset like this (which originally is read in from a .csv):</p>
  
  <pre><code>data = pd.DataFrame({'id': [1,2,3,1,2,3],
                       'time':['2017-01-01 12:00:00','2017-01-01 12:00:00','2017-01-01 12:00:00',
                            '2017-01-01 12:10:00','2017-01-01 12:10:00','2017-01-01 12:10:00'],
                       'value': [10,11,12,10,12,13]})
  </code></pre>
  
  <p>=></p>
  
  <pre><code>    id  time                    value
  0   1   2017-01-01 12:00:00     10
  1   2   2017-01-01 12:00:00     11
  2   3   2017-01-01 12:00:00     12
  3   1   2017-01-01 12:10:00     10
  4   2   2017-01-01 12:10:00     12
  5   3   2017-01-01 12:10:00     13
  </code></pre>
  
  <p>Time is identical for all IDs in each observation period. The series goes on like that for many observations, i.e. every ten minutes. </p>
  
  <p>I want the number of total changes in the <code>value</code> column by id between consecutive times. For example: For id=1 there is no change (result: 0). For id=2 there is one change (result: 1).
  Inspired by this post, I have tried taking differences:
  <a href="https://stackoverflow.com/questions/30196063/determining-when-a-column-value-changes-in-pandas-dataframe">Determining when a column value changes in pandas dataframe</a></p>
  
  <p>This is what I've come up so far (not working as expected):</p>
  
  <pre><code>data = data.set_index(['id', 'time']) # MultiIndex 
  grouped = data.groupby(level='id') 
  data['diff'] = grouped['value'].diff()
  data.loc[data['diff'].notnull(), 'diff'] = 1
  data.loc[data['diff'].isnull(), 'diff'] = 0
  grouped['diff'].sum()
  </code></pre>
  
  <p>However, this will just be the sum of occurrences for each id.</p>
  
  <p>Since my dataset is huge (and wont fit into memory), the solution should be as fast as possible. ( This is why I use a MultiIndex on id + time. I expect significant speedup because optimally the data need not be shuffled anymore.)</p>
  
  <p>Moreover, I have come around dask dataframes which are very similar to pandas dfs. A solution making use of them would be fantastic. </p>