pandas 活动研究 [英] Event Study in Pandas

查看:82
本文介绍了 pandas 活动研究的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个时间序列:

Suppose I have a time series as so:

pd.Series(np.random.rand(20), index=pd.date_range("1990-01-01",periods=20))

哪个给

1990-01-01    0.018363
1990-01-02    0.288625
1990-01-03    0.460708
1990-01-04    0.663063
1990-01-05    0.434250
1990-01-06    0.504893
1990-01-07    0.587743
1990-01-08    0.412223
1990-01-09    0.604656
1990-01-10    0.960338
1990-01-11    0.606765
1990-01-12    0.110480
1990-01-13    0.671683
1990-01-14    0.178488
1990-01-15    0.458074
1990-01-16    0.219303
1990-01-17    0.172665
1990-01-18    0.429534
1990-01-19    0.505891
1990-01-20    0.242567
Freq: D, dtype: float64

假设活动日期为1990年1月5日和1990年1月15日.我想像这样围绕事件将数据细分为长度为(-2,+ 2)的窗口:

Suppose the event date is on 1990-01-05 and 1990-01-15. I want to subset the data down to a window of length (-2,+2) around the event like this:

1990-01-03    0.460708
1990-01-04    0.663063
1990-01-05    0.434250
1990-01-06    0.504893
1990-01-07    0.587743
1990-01-13    0.671683
1990-01-14    0.178488
1990-01-15    0.458074
1990-01-16    0.219303
1990-01-17    0.172665
Freq: D, dtype: float64

我应该怎么做?

推荐答案

我认为您可以使用Series. Series.loc.html"rel =" nofollow noreferrer> loc :

I think you can use concat all Series created by list comprehension with loc:

date1 = pd.to_datetime('1990-01-05')
date2 = pd.to_datetime('1990-01-15')
window = 2

dates = [date1, date2]

s1 = pd.concat([s.loc[date - pd.Timedelta(window, unit='d'): 
                      date + pd.Timedelta(window, unit='d')] for date in dates])
print (s1)
1990-01-03    0.284356
1990-01-04    0.997019
1990-01-05    0.293225
1990-01-06    0.451379
1990-01-07    0.743209
1990-01-13    0.254926
1990-01-14    0.339728
1990-01-15    0.793124
1990-01-16    0.121002
1990-01-17    0.930924
dtype: float64

这篇关于 pandas 活动研究的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆