I want a dataframe representation of of a rolling window. Instead of performing some operation on a rolling window, I want a dataframe where the window is represented in another dimension. This could be as a pd.Panel
or np.array
or a pd.DataFrame
with a pd.MultiIndex
.
Setup
import pandas as pd
import numpy as np
np.random.seed([3,1415])
df = pd.DataFrame(np.random.rand(10, 3).round(2),
columns=['A', 'B', 'C'],
index=list('abcdefghij'))
print df
A B C
a 0.44 0.41 0.46
b 0.47 0.46 0.02
c 0.85 0.82 0.78
d 0.76 0.93 0.83
e 0.88 0.93 0.72
f 0.12 0.15 0.20
g 0.44 0.10 0.28
h 0.61 0.09 0.84
i 0.74 0.87 0.69
j 0.38 0.23 0.44
Expected Output
For a window = 2
I'd expect the result to be.
0 1
A B C A B C
a 0.44 0.41 0.46 0.47 0.46 0.02
b 0.47 0.46 0.02 0.85 0.82 0.78
c 0.85 0.82 0.78 0.76 0.93 0.83
d 0.76 0.93 0.83 0.88 0.93 0.72
e 0.88 0.93 0.72 0.12 0.15 0.20
f 0.12 0.15 0.20 0.44 0.10 0.28
g 0.44 0.10 0.28 0.61 0.09 0.84
h 0.61 0.09 0.84 0.74 0.87 0.69
i 0.74 0.87 0.69 0.38 0.23 0.44
I'm not determined to have the layout presented this way, but this is the information I want. I'm looking for the most efficient way to get at this.
What I've done so far
I've experimented with using shift
in varying ways but it feels clunky. This is what I use to produce the output above:
print pd.concat([df, df.shift(-1)], axis=1, keys=[0, 1]).dropna()