简体   繁体   中英

pandas rolling correlation with a subset

If I want a rolling correlation of a series with a particular subset, is this possible with pandas rolling method without using for loop? An example would be something like this.

# given this sample dataframe
df = pd.DataFrame(columns = ['a'])
df['a'] = [0.01, 0.02, -0.21, 0.05]

# rolling corr example
df.rolling(10).corr(df.iloc[-10:,:])
  • I was able to implement a rolling correlation with this function
def func(x):
    a = [0.002, -0.025, 0.0037]
    return np.corrcoef(x,a)[0,1]

df.rolling(3).apply(func)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM