<h4 dir="auto">well, my function's input is ndarray-like and it's output is a scaler. The question is I have no method to apply this function on rolling object.</h4>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="from sklearn.decomposition import PCA

df = pd.DataFrame({&quot;A&quot;: [1, 2, 3, 4, 5, 6, 7, 8], &quot;B&quot;: [2, 3, 4, 5, 6, 7, 8, 9]})

def first_variance(X):
    pca = PCA(n_components=1)
    pca.fit(X)
    return pca.explained_variance_

df.rolling(4).apply(first_variance)
"><pre class="notranslate"><span class="pl-k">from</span> <span class="pl-s1">sklearn</span>.<span class="pl-s1">decomposition</span> <span class="pl-k">import</span> <span class="pl-v">PCA</span>

<span class="pl-s1">df</span> <span class="pl-c1">=</span> <span class="pl-s1">pd</span>.<span class="pl-v">DataFrame</span>({<span class="pl-s">"A"</span>: [<span class="pl-c1">1</span>, <span class="pl-c1">2</span>, <span class="pl-c1">3</span>, <span class="pl-c1">4</span>, <span class="pl-c1">5</span>, <span class="pl-c1">6</span>, <span class="pl-c1">7</span>, <span class="pl-c1">8</span>], <span class="pl-s">"B"</span>: [<span class="pl-c1">2</span>, <span class="pl-c1">3</span>, <span class="pl-c1">4</span>, <span class="pl-c1">5</span>, <span class="pl-c1">6</span>, <span class="pl-c1">7</span>, <span class="pl-c1">8</span>, <span class="pl-c1">9</span>]})

<span class="pl-k">def</span> <span class="pl-en">first_variance</span>(<span class="pl-v">X</span>):
    <span class="pl-s1">pca</span> <span class="pl-c1">=</span> <span class="pl-v">PCA</span>(<span class="pl-s1">n_components</span><span class="pl-c1">=</span><span class="pl-c1">1</span>)
    <span class="pl-s1">pca</span>.<span class="pl-en">fit</span>(<span class="pl-v">X</span>)
    <span class="pl-k">return</span> <span class="pl-s1">pca</span>.<span class="pl-s1">explained_variance_</span>

<span class="pl-s1">df</span>.<span class="pl-en">rolling</span>(<span class="pl-c1">4</span>).<span class="pl-en">apply</span>(<span class="pl-s1">first_variance</span>)</pre></div>
<h4 dir="auto">The domo is incorrect because the "apply" method only can apply by index or column.</h4>
<p dir="auto">In <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="116942772" data-permission-text="Title is private" data-url="https://github.com/pandas-dev/pandas/issues/11603" data-hovercard-type="pull_request" data-hovercard-url="/pandas-dev/pandas/pull/11603/hovercard?comment_id=162113949&amp;comment_type=issue_comment" href="https://github.com/pandas-dev/pandas/pull/11603#issuecomment-162113949">#11603 (comment)</a> (the main PR implementing the deferred API for rolling / expanding / ewm), we discussed how to specify table-wise <code class="notranslate">apply</code>s. <code class="notranslate">Groupby.apply(f)</code> feeds the entire group (all columns) to <code class="notranslate">f</code>. For backwards-compatibility, <code class="notranslate">.rolling(n).apply(f)</code> needed to be column-wise.</p>
<p dir="auto"><a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="116942772" data-permission-text="Title is private" data-url="https://github.com/pandas-dev/pandas/issues/11603" data-hovercard-type="pull_request" data-hovercard-url="/pandas-dev/pandas/pull/11603/hovercard?comment_id=162116556&amp;comment_type=issue_comment" href="https://github.com/pandas-dev/pandas/pull/11603#issuecomment-162116556">#11603 (comment)</a> mentions a possible API like what I added for <code class="notranslate">.style</code></p>
<ul dir="auto">
<li><code class="notranslate">axis=0</code>: apply to each column independently</li>
<li><code class="notranslate">axis=1</code>: apply to each row independently</li>
<li><code class="notranslate">axis=None</code>: apply the supplied function to the entire table</li>
</ul>
<p dir="auto">So it'd be <code class="notranslate">df.rolling(n).apply(f, axis=None)</code>.<br>
Do people like the axis=0 / 1 / None idiom? Is it obvious enough?</p>
<p dir="auto">This is prompted by <a class="user-mention notranslate" data-hovercard-type="user" data-hovercard-url="/users/josef-pkt/hovercard" data-octo-click="hovercard-link-click" data-octo-dimensions="link_type:self" href="https://github.com/josef-pkt">@josef-pkt</a>'s <a href="https://groups.google.com/forum/#!topic/pydata/FcAT8LBPmlg" rel="nofollow">post on the mailinglist</a>. Needing a rolling OLS.</p>
<p dir="auto">An example:</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="In [2]: import numpy as np
   ...: import pandas as pd
   ...:
   ...: np.random.seed(0)
   ...: df = pd.DataFrame(np.random.randint(0, 10, size=(10, 2)), columns=[&quot;A&quot;, &quot;B&quot;])
   ...: df
   ...:
Out[2]:
   A  B
0  5  0
1  3  3
2  7  9
3  3  5
4  2  4
5  7  6
6  8  8
7  1  6
8  7  7
9  8  1"><pre class="notranslate"><span class="pl-v">In</span> [<span class="pl-c1">2</span>]: <span class="pl-s1">import</span> <span class="pl-s1">numpy</span> <span class="pl-k">as</span> <span class="pl-s1">np</span>
   ...: <span class="pl-k">import</span> <span class="pl-s1">pandas</span> <span class="pl-k">as</span> <span class="pl-s1">pd</span>
   ...:
   ...: <span class="pl-s1">np</span>.<span class="pl-s1">random</span>.<span class="pl-en">seed</span>(<span class="pl-c1">0</span>)
   ...: <span class="pl-s1">df</span> <span class="pl-c1">=</span> <span class="pl-s1">pd</span>.<span class="pl-v">DataFrame</span>(<span class="pl-s1">np</span>.<span class="pl-s1">random</span>.<span class="pl-en">randint</span>(<span class="pl-c1">0</span>, <span class="pl-c1">10</span>, <span class="pl-s1">size</span><span class="pl-c1">=</span>(<span class="pl-c1">10</span>, <span class="pl-c1">2</span>)), <span class="pl-s1">columns</span><span class="pl-c1">=</span>[<span class="pl-s">"A"</span>, <span class="pl-s">"B"</span>])
   ...: <span class="pl-s1">df</span>
   ...:
<span class="pl-v">Out</span>[<span class="pl-c1">2</span>]:
   <span class="pl-v">A</span>  <span class="pl-v">B</span>
<span class="pl-c1">0</span>  <span class="pl-c1">5</span>  <span class="pl-c1">0</span>
<span class="pl-c1">1</span>  <span class="pl-c1">3</span>  <span class="pl-c1">3</span>
<span class="pl-c1">2</span>  <span class="pl-c1">7</span>  <span class="pl-c1">9</span>
<span class="pl-c1">3</span>  <span class="pl-c1">3</span>  <span class="pl-c1">5</span>
<span class="pl-c1">4</span>  <span class="pl-c1">2</span>  <span class="pl-c1">4</span>
<span class="pl-c1">5</span>  <span class="pl-c1">7</span>  <span class="pl-c1">6</span>
<span class="pl-c1">6</span>  <span class="pl-c1">8</span>  <span class="pl-c1">8</span>
<span class="pl-c1">7</span>  <span class="pl-c1">1</span>  <span class="pl-c1">6</span>
<span class="pl-c1">8</span>  <span class="pl-c1">7</span>  <span class="pl-c1">7</span>
<span class="pl-c1">9</span>  <span class="pl-c1">8</span>  <span class="pl-c1">1</span></pre></div>
<p dir="auto">For a concrete example, get the table-wise max (this is equivalent to <code class="notranslate">df.rolling(4).max().max(1)</code>)</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="In [10]: df.rolling(4).apply(np.max, axis=None)
Out[10]:
0    NaN
1    NaN
2    NaN
3    9.0
4    9.0
5    9.0
6    8.0
7    8.0
8    8.0
9    8.0
dtype: float64"><pre class="notranslate"><span class="pl-v">In</span> [<span class="pl-c1">10</span>]: <span class="pl-s1">df</span>.<span class="pl-en">rolling</span>(<span class="pl-c1">4</span>).<span class="pl-en">apply</span>(<span class="pl-s1">np</span>.<span class="pl-s1">max</span>, <span class="pl-s1">axis</span><span class="pl-c1">=</span><span class="pl-c1">None</span>)
<span class="pl-v">Out</span>[<span class="pl-c1">10</span>]:
<span class="pl-c1">0</span>    <span class="pl-v">NaN</span>
<span class="pl-c1">1</span>    <span class="pl-v">NaN</span>
<span class="pl-c1">2</span>    <span class="pl-v">NaN</span>
<span class="pl-c1">3</span>    <span class="pl-c1">9.0</span>
<span class="pl-c1">4</span>    <span class="pl-c1">9.0</span>
<span class="pl-c1">5</span>    <span class="pl-c1">9.0</span>
<span class="pl-c1">6</span>    <span class="pl-c1">8.0</span>
<span class="pl-c1">7</span>    <span class="pl-c1">8.0</span>
<span class="pl-c1">8</span>    <span class="pl-c1">8.0</span>
<span class="pl-c1">9</span>    <span class="pl-c1">8.0</span>
<span class="pl-s1">dtype</span>: <span class="pl-s1">float64</span></pre></div>
<p dir="auto">A real example is something like a rolling OLS:</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="import statsmodels.api as sm
f = lambda x: sm.OLS.from_formula('A ~ B', data=x).fit()  # wrong, but w/e

df.rolling(5).apply(f, axis=None)"><pre class="notranslate"><span class="pl-k">import</span> <span class="pl-s1">statsmodels</span>.<span class="pl-s1">api</span> <span class="pl-k">as</span> <span class="pl-s1">sm</span>
<span class="pl-s1">f</span> <span class="pl-c1">=</span> <span class="pl-k">lambda</span> <span class="pl-s1">x</span>: <span class="pl-s1">sm</span>.<span class="pl-v">OLS</span>.<span class="pl-en">from_formula</span>(<span class="pl-s">'A ~ B'</span>, <span class="pl-s1">data</span><span class="pl-c1">=</span><span class="pl-s1">x</span>).<span class="pl-en">fit</span>()  <span class="pl-c"># wrong, but w/e</span>

<span class="pl-s1">df</span>.<span class="pl-en">rolling</span>(<span class="pl-c1">5</span>).<span class="pl-en">apply</span>(<span class="pl-s1">f</span>, <span class="pl-s1">axis</span><span class="pl-c1">=</span><span class="pl-c1">None</span>)</pre></div>