title: Speeding up group-wise differencing in Pandas
body: <p>Consider the following <a href="https://stackoverflow.com/a/28357227/283296">solution</a> to computing a within-group diff in Pandas:</p>
  
  <pre><code>df =  df.set_index(['ticker', 'date']).sort_index()[['value']]
  df['diff'] = np.nan
  idx = pd.IndexSlice
  
  for ix in df.index.levels[0]:
      df.loc[ idx[ix,:], 'diff'] = df.loc[idx[ix,:], 'value' ].diff()
  </code></pre>
  
  <p>For:</p>
  
  <pre><code>&gt; df
     date ticker  value
  0    63      C   1.65
  1    88      C  -1.93
  2    22      C  -1.29
  3    76      A  -0.79
  4    72      B  -1.24
  5    34      A  -0.23
  6    92      B   2.43
  7    22      A   0.55
  8    32      A  -2.50
  9    59      B  -1.01
  </code></pre>
  
  <p>It returns: </p>
  
  <pre><code>&gt; df
               value  diff
  ticker date             
  A      22     0.55   NaN
         32    -2.50 -3.05
         34    -0.23  2.27
         76    -0.79 -0.56
  B      59    -1.01   NaN
         72    -1.24 -0.23
         92     2.43  3.67
  C      22    -1.29   NaN
         63     1.65  2.94
         88    -1.93 -3.58
  </code></pre>
  
  <p>The solution does not scale well for large dataframes. It takes minutes for a dataframe with a shape <code>(405344,2)</code>. This is presumably the case because I am iterating through each value for the first level in the main loop.</p>
  
  <p>Is there any way of speeding this up in Pandas? Is looping through index values  a good way of solving this problem? Could <code>numba</code> perhaps be used for this?</p>