具有以下时间序列:
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 时间序列进行切片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
如何在 Python 中将货币字符串转换为浮点数?How do I convert a currency string to a floating point number in Python?(如何在 Python 中将货币字符串转换为浮点数?)
在 Pandas 中解析多索引 Excel 文件Parsing a Multi-Index Excel File in Pandas(在 Pandas 中解析多索引 Excel 文件)
pandas 时间序列 between_datetime 函数?pandas timeseries between_datetime function?( pandas 时间序列 between_datetime 函数?)
pandas 重新采样到每月的特定工作日pandas resample to specific weekday in month( pandas 重新采样到每月的特定工作日)
在 Pandas 中合并/组合两个具有不同频率时间序列Merging/combining two dataframes with different frequency time series indexes in Pandas?(在 Pandas 中合并/组合两个具有不同频率时间序列索
Python - 如何标准化时间序列数据Python - how to normalize time-series data(Python - 如何标准化时间序列数据)