title: Dynamically Change Lookback Period
body: <p>I am trying to dynamically adjust the lookback period of a pandas dataframe to run regressions on different lengths of stock data. As an easy example take an MA cross.</p>
  
  <pre><code>date        Prices  Diff    signal
  
  20150101    8.5     -1.5    FALSE
  20150101    11.5    0.3     TRUE 
  20150102    14.5    4.5     FALSE
  20150103    16.67   3.66    FALSE 
  20150104    18      2       FALSE
  20150105    18.5    0.5     FALSE 
  20150106    18.17   -2.17   TRUE 
  20150107    17      -3      FALSE
  </code></pre>
  
  <p>Diff is the difference between 2 moving averages and signal identifies a cross with true/false. Numbers are arbitrary, just examples. There could be 10, 50, 100, etc rows between each signal.</p>
  
  <p>Now I would like to run a regression on the prices for whatever length exists between signals, so at row 6 I would want prices[-4:], and row 8 I would want prices[-1:].</p>
  
  <p>Could anyone help me out with this problem? Looping backwards in each row until I find the latest signal seems inefficient. Should I simply assign an index value to a variable whenever a signal occurs, and use that variable to define the lookback period? I'm still relatively new to Python and not sure how to go about this. </p>
  
  <p>Any help would be appreciated. Thank you!</p>