slice pandas timeseries on date +/- 2 business days(在日期 +/- 2 个工作日内对 pandas 时间序列进行切片)
本文介绍了在日期 +/- 2 个工作日内对 pandas 时间序列进行切片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
具有以下时间序列:
In [65]: p
Out[65]:
Date
2008-06-02 125.20
2008-06-03 124.47
2008-06-04 124.40
2008-06-05 126.89
2008-06-06 122.84
2008-06-09 123.14
2008-06-10 122.53
2008-06-11 120.73
2008-06-12 121.19
Name: SPY
如何在特定日期 +/- 2 个相邻(工作日)进行切片,即如果 d = '2008-06-06':
how can I slice on a specfic date +/- 2 neighbouring (business) days, so ie if d = '2008-06-06':
-2 2008-06-04 124.40
-1 2008-06-05 126.89
0 2008-06-06 122.84
1 2008-06-09 123.14
2 2008-06-10 122.53
推荐答案
你可以使用索引方法get_loc
,然后切片:
You could use the index method get_loc
, and then slice:
d = pd.to_datetime('2008-06-06')
loc = s.index.get_loc(d)
In [12]: loc
Out[12]: 4
In [13]: s[loc-2:loc+3]
Out[13]:
2008-06-04 124.40
2008-06-05 126.89
2008-06-06 122.84
2008-06-09 123.14
2008-06-10 122.53
Name: SPY
.
如果你只是在两天内对这些感兴趣:
In [14]: dt = datetime.timedelta(1)
In [15]: s[d - 2*dt:d + 2*dt]
Out[15]:
2008-06-04 124.40
2008-06-05 126.89
2008-06-06 122.84
Name: SPY
这篇关于在日期 +/- 2 个工作日内对 pandas 时间序列进行切片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
沃梦达教程
本文标题为:在日期 +/- 2 个工作日内对 pandas 时间序列进行切


猜你喜欢
- 如何在 python3 中将 OrderedDict 转换为常规字典 2022-01-01
- 沿轴计算直方图 2022-01-01
- pytorch 中的自适应池是如何工作的? 2022-07-12
- 如何在 Python 的元组列表中对每个元组中的第一个值求和? 2022-01-01
- python-m http.server 443--使用SSL? 2022-01-01
- 分析异常:路径不存在:dbfs:/databricks/python/lib/python3.7/site-packages/sampleFolder/data; 2022-01-01
- padding='same' 转换为 PyTorch padding=# 2022-01-01
- python check_output 失败,退出状态为 1,但 Popen 适用于相同的命令 2022-01-01
- 使用Heroku上托管的Selenium登录Instagram时,找不到元素';用户名'; 2022-01-01
- 如何将一个类的函数分成多个文件? 2022-01-01