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>The <a href="https://github.com/EelcoHoogendoorn/Numpy_arraysetops_EP" rel="nofollow">numpy_indexed</a> package (disclaimer: I am its author) contains efficient and flexible functionality for these kind of grouping operations:</p>

<pre><code>import numpy_indexed as npi
unique_ids, diffed_time_groups = npi.group_by(keys=ids, values=times, reduction=np.diff)
</code></pre>

<p>Unlike pandas, it does not require a specialized datastructure just to perform this kind of rather elementary operation.</p>

python arrays numpy