2017-08-24 77 views
1

我有一个合理大小的时间序列数据DataFrame,我希望以合理的格式进行滚动成对关联数据。简化熊猫中的滚动关联输出为单个索引DataFrame

熊猫有一个非常有趣的“滚动”功能,做正确的计算

dfCorrelations = dfReturns.rolling(correlation_window).corr() 

但输出时间序列相关性的网格是不方便我在给定日期以后使用(样本输出的一个子集显示)。

enter image description here

有没有办法做同样的计算,而是要一个简单的时间序列数据帧只与独特,非对角线相关的输出?与列索引说,看起来像

['III LN x ABN NA', 'III LN x AGN NA', 'III LN x AGS BB', 'ABN NA x AGN NA', 'ABN NA x AGS BB', ...] 

回答

1
from itertools import combinations 

# Create sample dataset. 
idx = pd.MultiIndex(
    levels=[[u'2017-1-1', u'2017-1-2'], [u'A', u'B', u'C']], 
    labels=[[0, 0, 0, 1, 1, 1], [0, 1, 2, 0, 1, 2]], 
    names=[u'date', u'ticker']) 
df = pd.DataFrame(np.random.randn(6, 3), index=idx, columns=list('ABC')) 
for tup in zip(range(6), range(3) * 2): 
    df.iloc[tup] = 1 

>>> df 
         A   B   C 
date  ticker        
2017-1-1 A  1.000000 0.440276 -1.087536 
     B  -0.809949 1.000000 -0.548897 
     C  0.922866 -0.788699 1.000000 
2017-1-2 A  1.000000 -0.106493 0.034319 
     B  0.080990 1.000000 0.218323 
     C  0.051651 -0.680358 1.000000 

# Unstack and remove duplicates. 
tickers = df.columns.tolist() 
df = df.unstack().sort_index(axis=1) 
pairs = df.columns.get_values().tolist() 
df.columns = ["{0} vs. {1}".format(*pair) for pair in pairs] 
mask = [n for n, pair in enumerate(pairs) if pair in list(combinations(tickers, 2))] 
df = df.iloc[:, mask] 
>>> df 
      A vs. B A vs. C B vs. C 
date         
2017-1-1 -0.809949 0.922866 -0.788699 
2017-1-2 0.080990 0.051651 -0.680358 
+0

谢谢!非常有效的解决方案。 – rhaskett