Python 无重叠的滚动轴承

Python 无重叠的滚动轴承,python,pandas,correlation,Python,Pandas,Correlation,我有几个系列的价格回报,我想计算滚动N天的相关性,以确保日期之间没有重叠,即,如果我的第一个相关性矩阵属于[2000-04-05-2000-06-04],那么下一个相关性矩阵应该属于[2000-06-05-2000-08-04]。使用传统的df.rolling(window=window).corr(df,pairwise=True)将返回重叠的日期 我知道,对滚动方法的结果进行切片可以得到我想要的结果,但这意味着我们正在使用时间来计算我不会使用的相关性,这导致了资源的浪费 有什么建议吗 更新:

我有几个系列的价格回报,我想计算滚动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
一种(多种)方法是用批号标记行。如何批处理取决于您。然后使用groupby apply和定义的函数来计算相关性

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')

df['batch'] = np.arange(n) // 20

def process_batch(dg):
    return pd.DataFrame([[
                        dg['date'].min(),
                        dg['date'].max(),
                        dg[['A','B']].corr().values[0][1]
                        ]], columns=['date_min', 'date_max', 'corr'])

df.groupby('batch').apply(process_batch).reset_index(1, drop=True)
结果:

        date_min   date_max      corr
batch                                
0     2000-01-01 2000-01-20 -0.403241
1     2000-01-21 2000-02-09 -0.091487
2     2000-02-10 2000-02-29  0.091835
3     2000-03-01 2000-03-20  0.029466
4     2000-03-21 2000-04-09  0.100756
重新采样
您可以使用
pd.DataFrame.resample
使用
“20D”
指定20天的时间规则。使用
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

groupby


还可以明确说明要关联的列:

df.resample("20D", on='date').apply(lambda d: d.A.corr(d.B))
安装程序
调试 输出
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
df.resample("20D", on='date').apply(lambda d: d.A.corr(d.B))
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')
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)
                           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