I don't think Pandas has a built in function for what I want to do. Let's say I have 2 dfs each with 1000 rows. I want to:
- Calculate the rank of the first 5 rows for column 0 of df1
- Calculate the rank of the first 5 rows for column 0 of df2
- Find the correlation of the two columns
- Put this value in a 3rd df
I would like to do this for all rolling windows of 5 and for all pairs of columns.
In my past, I have tried to build custom rolling windows, by doing for loops. I would create a smaller df, do the operation on it, then create another small df, do the operation it, etc... My code was EXTREMELY slow.
For example, I would have something like:
df11 = data
df22 = data
for i in range(df.shape[0] - 4):
for j in range(df.shape[1]):
df1 = df11[i:i+5, j].rank()
df2 = df22[i:i+5, j].rank()
df3[i + 4, j] = corr(df1, df2)
I know the code above is not right syntax wise but you get the point.
But looping through the dataframe was extremely slow. And having 2 loops, would be even slower. I'd also have to keep track of the original dfs, and the two smaller dfs, through time. I wonder if there is a faster way...