熊猫滚动corr,没有重叠

Yuc*_*uca 2 python correlation pandas

我有几个价格回报系列,我想以一种在日期之间没有重叠的方式来计算N天的滚动相关性,即,如果我的第一个相关性矩阵属于[2000-04-05-2000-06- 04],则下一个相关矩阵应属于[2000-06-05-2000-08-04]。使用常规的df.rolling(window = window).corr(df,pairwise = True)将返回重叠的日期。

我知道将滚动方法的结果切成薄片会得到我想要的东西,但这意味着我们浪费时间来计算我不会使用的相关性,从而浪费了资源。

有什么建议么?

更新:

这是输入的示例:

在此处输入图片说明

更新2:

outputs for pd.show_versions()
INSTALLED VERSIONS
------------------
commit: None
python: 3.6.3.final.0
python-bits: 64
OS: Windows
OS-release: 10
machine: AMD64
processor: Intel64 Family 6 Model 63 Stepping 2, GenuineIntel
byteorder: little
LC_ALL: None
LANG: en
LOCALE: None.None

pandas: 0.20.3
pytest: 3.2.1
pip: 9.0.1
setuptools: 36.5.0.post20170921
Cython: 0.26.1
numpy: 1.14.5
scipy: 0.19.1
xarray: None
IPython: 6.1.0
sphinx: 1.6.3
patsy: 0.4.1
dateutil: 2.6.1
pytz: 2017.2
blosc: None
bottleneck: 1.2.1
tables: 3.4.2
numexpr: 2.6.2
feather: None
matplotlib: 2.1.0
openpyxl: 2.4.8
xlrd: 1.1.0
xlwt: 1.3.0
xlsxwriter: 1.0.2
lxml: 4.1.0
bs4: 4.6.0
html5lib: 0.999999999
sqlalchemy: 1.1.13
pymysql: None
psycopg2: None
jinja2: 2.9.6
s3fs: None
pandas_gbq: None
pandas_datareader: None
Run Code Online (Sandbox Code Playgroud)

piR*_*red 6

resample

您可以使用pd.DataFrame.resample来指定20天的时间规则"20D"。使用on参数指定要重新采样的列。生成的resample对象类似于该groupby对象,并且可以处理apply方法。

def dcorr(df, n):
    return df.resample(f"{n}D", on='date').apply(lambda d: d.corr())

dcorr(df, 20)

                     A         B
date                            
2000-01-01 A  1.000000  0.241121
           B  0.241121  1.000000
2000-01-21 A  1.000000  0.083664
           B  0.083664  1.000000
2000-02-10 A  1.000000  0.432988
           B  0.432988  1.000000
2000-03-01 A  1.000000 -0.269869
           B -0.269869  1.000000
2000-03-21 A  1.000000 -0.188370
           B -0.188370  1.000000
Run Code Online (Sandbox Code Playgroud)

groupby

df.set_index('date').groupby(pd.Grouper(freq='20D')).corr()

                     A         B
date                            
2000-01-01 A  1.000000  0.241121
           B  0.241121  1.000000
2000-01-21 A  1.000000  0.083664
           B  0.083664  1.000000
2000-02-10 A  1.000000  0.432988
           B  0.432988  1.000000
2000-03-01 A  1.000000 -0.269869
           B -0.269869  1.000000
2000-03-21 A  1.000000 -0.188370
           B -0.188370  1.000000
Run Code Online (Sandbox Code Playgroud)

要么

df.set_index('date').groupby(pd.Grouper(freq='20D')).corr().unstack()[('A', 'B')]

date
2000-01-01    0.241121
2000-01-21    0.083664
2000-02-10    0.432988
2000-03-01   -0.269869
2000-03-21   -0.188370
Name: (A, B), dtype: float64
Run Code Online (Sandbox Code Playgroud)

您还可以明确显示要关联的列:

df.resample("20D", on='date').apply(lambda d: d.A.corr(d.B))
Run Code Online (Sandbox Code Playgroud)

设定

np.random.seed([3, 1415])

n = 100
df = pd.DataFrame(np.random.rand(n,2), columns=['A','B'])
df['date'] = pd.date_range('2000-01-01', periods=n, name='date')
Run Code Online (Sandbox Code Playgroud)

调试

import pandas as pd
import numpy as np

np.random.seed([3, 1415])

n = 100
df = pd.DataFrame(
    np.random.rand(n, 4),
    pd.date_range('2000-01-01', periods=n, name='date'),
    ['ABC','XYZ __', 'One', 'Two Three']
)


def dcorr(df, n):
    return df.resample(f"{n}D").apply(lambda d: d.corr())

dcorr(df, 20)
Run Code Online (Sandbox Code Playgroud)

输出值

                           ABC    XYZ __       One  Two Three
date                                                         
2000-01-01 ABC        1.000000 -0.029687  0.403720   0.078800
           XYZ __    -0.029687  1.000000 -0.231223  -0.333266
           One        0.403720 -0.231223  1.000000   0.330959
           Two Three  0.078800 -0.333266  0.330959   1.000000
2000-01-21 ABC        1.000000 -0.024610  0.206002  -0.059523
           XYZ __    -0.024610  1.000000 -0.601174  -0.101306
           One        0.206002 -0.601174  1.000000   0.149536
           Two Three -0.059523 -0.101306  0.149536   1.000000
2000-02-10 ABC        1.000000 -0.361072  0.156693  -0.040827
           XYZ __    -0.361072  1.000000 -0.077173  -0.232536
           One        0.156693 -0.077173  1.000000   0.343754
           Two Three -0.040827 -0.232536  0.343754   1.000000
2000-03-01 ABC        1.000000  0.204763 -0.013132   0.115202
           XYZ __     0.204763  1.000000 -0.339747  -0.206922
           One       -0.013132 -0.339747  1.000000   0.310002
           Two Three  0.115202 -0.206922  0.310002   1.000000
2000-03-21 ABC        1.000000  0.062841 -0.245393   0.233697
           XYZ __     0.062841  1.000000 -0.213742   0.341582
           One       -0.245393 -0.213742  1.000000   0.251169
           Two Three  0.233697  0.341582  0.251169   1.000000
Run Code Online (Sandbox Code Playgroud)