pandas - 计算具有循环依赖关系的两个Series的更有效方法

时间:2012-08-03 00:55:14

标签: python pandas

我有DataFrame代表股票回报。要分割调整收盘价,我有以下方法:

def returns(ticker, start=None, end=None):
    p = historical_prices(ticker, start, end, data='d', convert=True)
    d = historical_prices(ticker, start, end, data='v', convert=True)

    p['Dividends'] = d['Dividends']
    p['Dividends'].fillna(value=0, inplace=True)
    p['DivFactor'] = 1.
    p['SAClose'] = p['Close']

    records, fields = p.shape
    for t in range(1, records):
        p['SAClose'][t] = p['Adj Close'][t] / p['DivFactor'][t-1] + \
                          p['Dividends'][t-1]
        p['DivFactor'][t] = p['DivFactor'][t-1] * \
                            (1 - p['Dividends'][t-1] / p['SAClose'][t])

    p['Lagged SAClose'] = p['SAClose'].shift(periods=-1)
    p['Cash Return'] = p['Dividends'] / p['Lagged SAClose']
    p['Price Return'] = p['SAClose'] / p['Lagged SAClose'] - 1
    return p.sort_index()

注意SAClose(即拆分调整关闭)取决于滞后DivFactor值。反过来,DivFactor取决于滞后DivFactor值以及当前SAClose值。

上面的方法有效,但在循环部分却非常慢。在熊猫中我有更有效的方法吗?鉴于“循环”依赖(给定滞后不是真正的循环),我不确定如何进行常规系列数学或使用正常的移位操作(例如我对Cash Return)。

1 个答案:

答案 0 :(得分:3)

您可以尝试一次创建累积调整因子系列,然后您不需要循环:

(p['Dividends'].fillna(1.) + 1.).cumprod()