title: How to find times when a variable is below a certain value in pandas
body: <p>I have a dataframe in pandas (python) which is a measured variable from an experiment with a time index. I am looking to extract out the times when this values dips below a certain value. However the noise sometimes will result in the variable going above and below the threshold so I would also only like to find a new timepoint if the variable has then gone above another threshold. The code I've written so far is:</p>
  
  <pre><code>def findPriming(df,col,sphigh,splow):
      #start the counter and the pastPrime detector
      i = 1 # this ignores the first value but lets us check with the one before with no errors.
      currentlyPriming = False
      primeTimes = []
          #Right iteratre through the series here:
      while i &lt; range(len(df)):
          # If the value is above 20, everything is fine and its not priming
          if df[col].iloc[i] &gt; sphigh:
              currentlyPriming = False
  
          #If its below 16:
          elif df[col].iloc[i] &lt; splow:
              #Check if we are currently priming:
              if not currentlyPriming:
                  # We are now priming and haven't been before. So let's log it
                  primeTimes.append(df.index[i])
              # Now we are priming we need to set the flag!
              currentlyPriming = True
          # Nowincrement the counter
          i += 1  # Increment counter
  
      return primeTimes
  </code></pre>
  
  <p>But I can imagine this is horribly inefficient (and the fact it's taking forever to run would tell me the same).</p>
  
  <p>I was trying to think how to remove the two if's every data point but can't get it to work.</p>
  
  <p>Does anyone have any ideas on improvements? I've tried to search for similar code but can't seem to find anything.</p>
  
  <p>Edit to include an example of my dataframe:</p>
  
  <pre><code>DateTime                      Data
  2013-08-08 15:46:41           25.203461
  2013-08-08 15:46:51           23.241514
  2013-08-08 15:47:01           22.256216
  2013-08-08 15:47:11           21.256216
  2013-08-08 15:47:21           16.261763
  2013-08-08 15:47:31           13.249237
  2013-08-08 15:47:41           17.249237
  2013-08-08 15:47:51           18.238962
  2013-08-08 15:48:01           13.207640
  2013-08-08 15:48:11           20.207640
  </code></pre>
  
  <p>And a link to an example graph i've (badly) drawn [inlined --ed]</p>
  
  <p><img src="https://i.stack.imgur.com/zoG0P.png" alt="example image"> </p>