title: Rolling Correlation Calculation with Pandas or NumPy
body: <p>I am trying to find an efficient way for the following problem:</p>
  
  <p>Two data frames, each with the following data:<br>
  1st one: <code>id, date, value</code>  </p>
  
  <p>Sample Data:</p>
  
  <pre><code>id, date, value
  f130,200701,0.016196
  f130,200702,-0.027798
  f130,200703,-0.014868
  f130,200704,0.017801
  f130,200705,-0.032700
  f130,200706,0.049529
  f130,200707,0.011610
  f130,200708,-0.008145
  f130,200709,-0.001493
  f130,200710,0.009719
  f130,200711,-0.007775
  f130,200712,-0.007835
  f131,200701,0.044754
  f131,200702,0.004679
  f131,200703,-0.011824
  f131,200704,0.007252
  f131,200705,0.029877
  f131,200706,0.001748
  f131,200707,0.001047
  f131,200708,-0.003137
  f131,200709,0.001748
  f131,200710,0.006632
  f131,200711,-0.012136
  f131,200712,0.004914
  </code></pre>
  
  <p>2nd one: <code>id_2, date, value</code></p>
  
  <p>Sample Data:</p>
  
  <pre><code>id_2, date, value
  d_1,200701,0.026316
  d_1,200702,-0.004487
  d_1,200703,-0.027044
  d_1,200704,-0.035076
  d_1,200705,0.010288
  d_1,200706,-0.031908
  d_1,200707,-0.001403
  d_1,200708,0.009831
  d_1,200709,0.040334
  d_1,200710,0.018048
  d_1,200711,0.011819
  d_1,200712,0.000000
  d_2,200701,0.000000
  d_2,200702,0.028553
  d_2,200703,-0.037224
  d_2,200704,0.041284
  d_2,200705,0.151038
  d_2,200706,0.061236
  d_2,200707,0.001030
  d_2,200708,-0.042203
  d_2,200709,0.000000
  d_2,200710,0.006986
  d_2,200711,-0.018676
  d_2,200712,0.001087
  </code></pre>
  
  <p>What i need, is a rolling window correlation (rolling over <code>date</code> column) between the two <code>value</code> columns for all <code>id &amp; id_2</code> pairs<br>
  Essentially, my output should be: <code>"id vs id_2", date, corr</code>
  So, for <code>d1 vs f130</code>, for <code>200706</code>, i calculate the correlation between values from <code>d1</code> and <code>f130</code> starting from <code>200706</code> going back <code>6</code> months. The same for all pairs.
  Expected output:</p>
  
  <pre><code>id_pair, date, value
  d1_f130,200706,-0.375238392
  d1_f130,200707,-0.667154011
  d1_f130,200708,-0.636064899
  d1_f130,200709,-0.672029012
  d1_f130,200710,-0.653719992
  d1_f130,200711,-0.802893705
  d1_f130,200712,-0.03120143
  d1_f131,200706,0.870717009
  d1_f131,200707,0.61076152
  d1_f131,200708,0.400632396
  d1_f131,200709,0.05064842
  d1_f131,200710,0.087102168
  d1_f131,200711,-0.012306865
  d1_f131,200712,0.05170204
  d2_f130,200706,-0.170979922
  d2_f130,200707,-0.15363222
  d2_f130,200708,-0.089709021
  d2_f130,200709,-0.227564277
  d2_f130,200710,-0.252391258
  d2_f130,200711,0.94878745
  d2_f130,200712,0.619029635
  d2_f131,200706,0.358385975
  d2_f131,200707,0.952074283
  d2_f131,200708,0.930805345
  d2_f131,200709,0.919101445
  d2_f131,200710,0.904473885
  d2_f131,200711,0.47080201
  d2_f131,200712,0.640334152
  </code></pre>
  
  <p>Using for loops to iterate over ids and dates takes days... (number of id's ~15000, id_2 ~ 300, dates ~ 300)</p>
  
  <p>Any ideas?</p>