Efficient way for calculating selected differences in array
<p>I have two arrays as an output from a simulation script where one contains IDs and one times, i.e. something like:</p>

<pre><code>ids = np.array([2, 0, 1, 0, 1, 1, 2])
times = np.array([.1, .3, .3, .5, .6, 1.2, 1.3])
</code></pre>

<p>These arrays are always of the same size. Now I need to calculate the differences of <code>times</code>, but only for those times with the same <code>ids</code>. Of course, I can simply loop over the different <code>ids</code> an do</p>

<pre><code>for id in np.unique(ids):
    diffs = np.diff(times[ids==id])
    print diffs
    # do stuff with diffs
</code></pre>

<p>However, this is quite inefficient and the two arrays can be very large. Does anyone have a good idea on how to do that more efficiently?</p>

<p>I'm adding another answer, since, even though these things are possible in <code>numpy</code>, I think that the higher-level <a href="http://pandas.pydata.org/" rel="nofollow"><code>pandas</code></a> is much more natural for them.</p>

<p>In <code>pandas</code>, you could do this in one step, after creating a DataFrame:</p>

<pre><code>df = pd.DataFrame({'ids': ids, 'times': times})

df['diffs'] = df.groupby(df.ids).transform(pd.Series.diff)
</code></pre>

<p>This gives:  </p>

<pre><code>&gt;&gt;&gt; df
   ids  times  diffs
0    2    0.1    NaN
1    0    0.3    NaN
2    1    0.3    NaN
3    0    0.5    0.2
4    1    0.6    0.3
5    1    1.2    0.6
6    2    1.3    1.2
</code></pre>

python arrays numpy