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>You can use <code>array.argsort()</code> and ignore the values corresponding to change in ids:</p>

<pre><code>&gt;&gt;&gt; id_ind = ids.argsort(kind='mergesort')
&gt;&gt;&gt; times_diffs = np.diff(times[id_ind])
array([ 0.2, -0.2,  0.3,  0.6, -1.1,  1.2])
</code></pre>

<p>To see which values you need to discard, you could use a Counter to count the number of times per id (<code>from collections import Counter</code>)</p>

<p>or just sort ids, and see where its diff is nonzero: these are the indices where id change, and where you time diffs are irrelevant:</p>

<pre><code>times_diffs[np.diff(ids[id_ind]) == 0] # ids[id_ind] being the sorted indices sequence
</code></pre>

<p>and finally you can split this array with np.split and np.where:</p>

<pre><code>np.split(times_diffs, np.where(np.diff(ids[id_ind]) != 0)[0])
</code></pre>

<p>As you mentionned in your comment, <code>argsort()</code> default algorithm (quicksort) might not preserve order between equals times, so the <code>argsort(kind='mergesort')</code> option must be used.</p>

python arrays numpy