slicing series of panels
<p>I have a simple dataframe:</p>

<pre><code>&gt;&gt;&gt; df = pd.DataFrame(np.random.randint(0,5,(20, 2)), columns=['col1','col2'])
&gt;&gt;&gt; df['ind1'] = list('AAAAAABBBBCCCCCCCCCC')
&gt;&gt;&gt; df.set_index(['ind1'], inplace=True)
&gt;&gt;&gt; df

      col1  col2
ind1            
A        0     4
A        1     2
A        1     0
A        4     1
A        1     3
A        0     0
B        0     4
B        2     0
B        3     1
B        0     3
C        1     3
C        2     1
C        4     0
C        4     0
C        4     1
C        3     0
C        4     4
C        0     2
C        0     2
C        1     2
</code></pre>

<p>I am trying to get the rolling correlation coefficient of its two columns:</p>

<pre><code>&gt;&gt;&gt; df.groupby(level=0).rolling(3,min_periods=1).corr()

ind1
A    &lt;class 'pandas.core.panel.Panel'&gt;
Dimensions: ...
B    &lt;class 'pandas.core.panel.Panel'&gt;
Dimensions: ...
C    &lt;class 'pandas.core.panel.Panel'&gt;
Dimensions: ...
dtype: object
</code></pre>

<p>The problem is that the result is series of panels:</p>

<pre><code>&gt;&gt;&gt; type(df.groupby(level=0).rolling(3,min_periods=1).corr())

pandas.core.series.Series
</code></pre>

<p>I am able to get desired coefficient for each row separately...</p>

<pre><code>&gt;&gt;&gt; df.groupby(level=0).rolling(3,min_periods=1).corr()['A']

&lt;class 'pandas.core.panel.Panel'&gt;
Dimensions: 10 (items) x 2 (major_axis) x 2 (minor_axis)
Items axis: C to C
Major_axis axis: col1 to col2
Minor_axis axis: col1 to col2

&gt;&gt;&gt; df.groupby(level=0).rolling(3,min_periods=1).corr().loc['A'].ix[2]

          col1      col2
col1  1.000000 -0.866025
col2 -0.866025  1.000000

&gt;&gt;&gt; df.groupby(level=0).rolling(3,min_periods=1).corr().loc['A'].ix[2,'col1','col2']

-0.86602540378443849
</code></pre>

<p>...but I don't know how to slice the result (series of panels) in order to assign the results as a column to existing dataframe.  Something like:</p>

<pre><code>df['cor_coeff'] = df.groupby(level=0).rolling(3,min_periods=1).corr()['some slicing']
</code></pre>

<p>Any clues? Or a better way to get rolling correlation coefficients?</p>

<p>Your problem is that <a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.core.window.Rolling.corr.html" rel="nofollow"><code>.corr()</code></a> is being called without specifying the <code>other</code> argument.  Even though your dataframe only has two columns, Pandas doesn't know which correlation you actually want, so it calculates all possible correlations (<code>col1</code> x <code>col1</code>, <code>col1</code> x <code>col2</code>, <code>col2</code> x <code>col1</code>, <code>col2</code> x <code>col2</code>) and gives the results to you in a 2x2 datastructure.  If you want to get the results from one correlation, you need to specify the correlation you want by setting the base column and the <code>other</code> column.  If you weren't using <code>groupby</code> you'd just do it this way:</p>

<pre><code>df['col1'].rolling(min_periods=1, window=3).corr(other=g['col2'])
</code></pre>

<p>Since you're using <code>groupby</code>, you need to nest it in an <code>apply</code> clause with a lambda function (or you could move it into a separate function if you preferred):</p>

<pre><code>df.groupby(level=0).apply(lambda g: g['col1'].rolling(min_periods=1, window=3).corr(other=g['col2']))
</code></pre>

python pandas slice