title: Simple Python program of threshold detection too slow
body: <p>Python (and programming) newbie here. I wrote a code that goes through two arrays (x and y; x is linear time and y are measurements) to delete &quot;events&quot; that are smaller than a certain threshold. Each one is an array with shape (12000000,). Start and Stop are arrays of same shape ~(600,) which contain the time of beginning and end of each event, respectively. Threshold is a float.</p>
  <p>The code works but it is very slow. I am not sure if it's because of the use of np.where or if it's because I am having to loop through np.nanmax.</p>
  <p>Any tips on how to make this run faster?</p>
  <pre><code>
  def event_threshold (x, y, start, stop, threshold):
          
      result_start = []
      result_stop = []
      
      for i_start,i_stop in zip(start,stop): 
          
          start_x = np.where(x == i_start)[0][0]
          stop_x = np.where(x == i_stop)[0][0]
          
          if threshold &gt;= 0:
              if np.nanmax(y[start_x:stop_x]) &gt;= threshold:
                  #Add elements if cross positive threshold
                  result_start = np.append(result_start, i_start)
                  result_stop = np.append(result_stop, i_stop)
          else: #if threshold is negative
              if np.nanmin(y[start_x:stop_x]) &lt;= threshold:
                  #Add elements if cross negative threshold
                  result_start = np.append(result_start, i_start)
                  result_stop = np.append(result_stop, i_stop)
      return result_start, result_stop
  </code></pre>