title: How to use pandas rolling apply with a simple custom function?
body: <p>I have a function <code>func</code> that I want to apply to consecutive rows of a pandas dataframe. However, I get a <code>ValueError:</code> when I try to do it as below.</p>
  <pre><code>import numpy as np
  import pandas as pd
  
  def func(a: np.ndarray, b: np.ndarray) -&gt; float:
      &quot;&quot;&quot;Return the sum of sum of vectors a and b&quot;&quot;&quot;
      return np.sum(a) + np.sum(b)
  
  df = pd.DataFrame({&quot;a&quot;: [1, 2, 3, 4, 5], &quot;b&quot;: [10, 11, 12, 13, 14]})
  df.rolling(window=2, axis=1).apply(func)
  &gt;&gt;&gt;
  ValueError: Length of passed values is 2, index implies 5.
  </code></pre>
  <p>All I want to do is apply <code>func</code> on a rolling basis to consecutive rows (which is why I chose <code>window=2</code> above). The snippet below is a manual implementation of this.</p>
  <pre><code>func(df.iloc[0, :].values, df.iloc[1, :].values)
  &gt;&gt;&gt; 24
  func(df.iloc[1, :].values, df.iloc[2, :].values)
  &gt;&gt;&gt; 28
  </code></pre>
  <p>and so on.</p>
  <p>Note that the example I gave for <code>func</code> is just for illustrative purposes - I know that that you could use a simple <code>df.sum(axis=1) + df.shift(-1).sum(axis=1)</code> in this case. What I want to know is how you use rolling apply for custom functions in the general case.</p>