我有一个多索引数据框,其中包含日期和股票代码的索引。这是一个子集:
我想创建几个滞后变量。我已经想出如何创造一天的滞后期:
df['Number of Tweets [t-1]'] = df['Number of Tweets'].unstack().shift(1).stack()
我陷入困境的是创建一个滞后变量,它在第t-1到t-3天,或者t-1到t-7,或者t-1到t-30之间聚合(求和)值。例如,我想要一个名为“推文数量[t-1到t-3之和]”的列。我已经玩过 DateOffset 并查看了 resample 但是还没有比这更进一步。我似乎也无法在Cookbook中找到任何答案或文档中的示例有帮助。我正在转动这个轮子,所以我将不胜感激。
答案 0 :(得分:2)
对移位的数据使用pd.rolling_sum。要计算t-3到t-1的滚动总和,请使用窗口长度3并将数据移位1(如果未指定参数,则为默认值)。
from pandas import Timestamp
# Create series
s = pd.Series({(Timestamp('2015-03-30 00:00:00'), 'AAPL'): 2,
(Timestamp('2015-03-30 00:00:00'), 'IBM'): 3,
(Timestamp('2015-03-30 00:00:00'), 'TWTR'): 2,
(Timestamp('2015-03-31 00:00:00'), 'AAPL'): 6,
(Timestamp('2015-03-31 00:00:00'), 'IBM'): 2,
(Timestamp('2015-03-31 00:00:00'), 'TWTR'): 7,
(Timestamp('2015-04-01 00:00:00'), 'AAPL'): 3,
(Timestamp('2015-04-01 00:00:00'), 'IBM'): 1,
(Timestamp('2015-04-01 00:00:00'), 'TWTR'): 2,
(Timestamp('2015-04-02 00:00:00'), 'AAPL'): 6,
(Timestamp('2015-04-02 00:00:00'), 'IBM'): 8,
(Timestamp('2015-04-02 00:00:00'), 'TWTR'): 2,
(Timestamp('2015-04-06 00:00:00'), 'AAPL'): 4,
(Timestamp('2015-04-06 00:00:00'), 'IBM'): 2,
(Timestamp('2015-04-06 00:00:00'), 'TWTR'): 6,
(Timestamp('2015-04-07 00:00:00'), 'AAPL'): 3,
(Timestamp('2015-04-07 00:00:00'), 'IBM'): 7,
(Timestamp('2015-04-07 00:00:00'), 'TWTR'): 8})
# View the data more easily:
s.unstack()
AAPL IBM TWTR
Date
2015-03-30 2 3 2
2015-03-31 6 2 7
2015-04-01 3 1 2
2015-04-02 6 8 2
2015-04-06 4 2 6
2015-04-07 3 7 8
# Calculate a rolling sum on date t for dates t-3 through t-1:
result = pd.rolling_sum(s.unstack().shift(), window=3) # .shift() <=> .shift(1)
>>> result
AAPL IBM TWTR
Date
2015-03-30 NaN NaN NaN
2015-03-31 NaN NaN NaN
2015-04-01 NaN NaN NaN
2015-04-02 11 6 11
2015-04-06 15 11 11
2015-04-07 13 11 10
# Restack the data:
>>> result.stack()
2015-04-02 AAPL 11
IBM 6
TWTR 11
2015-04-06 AAPL 15
IBM 11
TWTR 11
2015-04-07 AAPL 13
IBM 11
TWTR 10