title: Faster rolling_apply on a Pandas DataFrame?
body: <p>Improving upon <a href="https://stackoverflow.com/questions/21040766/python-pandas-rolling-apply-two-column-input-into-function">this question</a> which provided a clever solution for applying a function over multiple columns in a DataFrame, I'm wondering if the solution can be further optimized for speed.</p>
  
  <p>Environment: Python 2.7.8, Pandas 14.1, Numpy 1.8.  </p>
  
  <p>Here's the example setup:</p>
  
  <pre><code>import pandas as pd
  import numpy as np
  import random
  
  def meanmax(ii,df):
      xdf = df.iloc[map(int,ii)]
      n = max(xdf['A']) + max(xdf['B'])
      return n / 2.0
  
  df  = pd.DataFrame(np.random.randn(2500,2)/10000, 
                      index=pd.date_range('2001-01-01',periods=2500),
                      columns=['A','B'])              
  df['ii'] = range(len(df))      
  
  res = pd.rolling_apply(df.ii, 26, lambda x: meanmax(x, df))
  </code></pre>
  
  <p>Note that the <code>meanmax</code> function is not pairwise, thus something like <code>rolling_mean(df['A'] + df['B'],26)</code> won't work.   </p>
  
  <p>However I can do something like:</p>
  
  <pre><code>res2 = (pd.rolling_max(df['A'],26) + pd.rolling_max(df['B'],26)) / 2
  </code></pre>
  
  <p>Which completes roughly 3000x faster:</p>
  
  <pre><code>%timeit res = pd.rolling_apply(df.ii, 26, lambda x: meanmax(x, df))
  1 loops, best of 3: 1 s per loop
  
  %timeit res2 = (pd.rolling_max(df['A'],26) + pd.rolling_max(df['B'],26)) / 2
  1000 loops, best of 3: 325 µs per loop
  </code></pre>
  
  <p>Is there anything better/equivalent than the second option above, given the example function and using <code>rolling_apply</code>?  While the second option is faster, it doesn't use a <code>rolling_apply</code>, which can be applied to a wider problem set</p>
  
  <p>Edit: Performance timing correction</p>