我认为rolling_apply 不能用于滚动关联,因为它似乎将 DataFrame 分解为一维数组。可能有更好的方法来做到这一点,但一种解决方案是让生成器自己为每个窗口生成一个切片:
def window(length, size=2, start=0):
while start + size <= length:
yield slice(start, start + size)
start += 1
然后循环遍历它..
In [144]: from pandas import DataFrame
...: import numpy as np
...:
...: df = DataFrame(np.arange(10).reshape(2,5).T, columns=['a','b'])
...:
...: df.iloc[0,1] = -1 #still perfect with ranked correlation, but not with pearson's r
...:
...: for w in window(len(df), size=3):
...: df_win = df.iloc[w,:]
...: spearman = df_win['a'].rank().corr(df_win['b'].rank())
...: pearson = df_win['a'].corr(df_win['b'])
...: print w.start, spearman, pearson
...:
0 1.0 0.917662935482
1 1.0 1.0
2 1.0 1.0