title: Speed up array interval comparison using NumPy
body: <p>I have a large set of data points taken in a certain time interval, and I am attempting to compute a weighted average of this data on a different time interval by looking at the overlaps between those two intervals. I have a successful method, described below, but it is rather slow. I'm looking for any suggestions as to how I could make this run faster.</p>
  
  <p>I have two sets of time intervals as well as the data points associated with one of those time intervals. I need to determine which of the data points from the first time intervals fall into the second time intervals. For example, if the following were my arrays:</p>
  
  <pre><code>start1 = np.array([1.,6.,11.,16.,21.,26.,31.,36.])
  stop1 = np.array([6.,11.,16.,21.,26,31.,36.,41.])
  start2 = np.array([1.,2.,3.,4.,5.,6.,7.,8.,9.,10.,11.,12.,13.,14.,15.,16.,17.,...,39.])
  stop2 = np.array([2.,3.,4.,5.,6.,7.,8.,9.,10.,11.,12.,13.,14.,15.,16.,17.,18.,...,40.])
  data1 = np.array([2.,4.,3.,8.,4.,7.,2.,6.])
  </code></pre>
  
  <p>Then my results would be:</p>
  
  <pre><code>[array([ 2.]), array([ 2.]), array([ 2.]), array([ 2.]), array([ 2.,  4.]), array([ 2.,  4.]), array([ 4.]), array([ 4.]), array([ 4.]), array([ 4.,  3.]), array([ 4.,  3.]), array([ 3.]), array([ 3.]), array([ 3.]), array([ 3.,  8.]), array([ 3.,  8.]), array([ 8.]), array([ 8.]), array([ 8.]), array([ 8.,  4.]), array([ 8.,  4.]), array([ 4.]), array([ 4.]), array([ 4.]), array([ 4.,  7.]), array([ 4.,  7.]), array([ 7.]), array([ 7.]), array([ 7.]), array([ 7.,  2.]), array([ 7.,  2.]), array([ 2.]), array([ 2.]), array([ 2.]), array([ 2.,  6.]), array([ 2.,  6.]), array([ 6.]), array([ 6.]), array([ 6.])]
  </code></pre>
  
  <p>This is the method that I am currently using:</p>
  
  <pre><code>intervals_data = []
  for i in range(0, len(start2)):
      intervals_data.append(data1[((stop1&gt;=start2[i]) &amp; (start1&lt;=stop2[i]))])
  </code></pre>
  
  <p>The problem that I am having is that these arrays are typically on the order of 25,000 elements, so this takes around 20 seconds per data set. I typically have a few hundred data sets, so it end up taking about an hour to run. </p>
  
  <p>Can anyone please point me towards a faster method? Thanks!</p>