本题使用Python-3.7
和pandas-0.23.4
。
我目前正在处理金融数据集,我需要仅检索每个交易日 08:15 到 13:45 之间的数据
变量设置
为了说明这一点,我有一个 DataFrame
变量和 DateTimeIndex
,声明为以下代码:
y = (
pd.DataFrame(columns=['x', 'y'])
.reindex(pd.date_range('20100101', '20100105', freq='1min'))
)
问题介绍
我想从 08:15 到 13:45 之间的每一 day
对数据进行切片。下面的代码似乎可以工作,但我认为它不是很 Pythonic,而且考虑到末尾的双重索引,它似乎不是很节省内存:
In [108]: y[y.index.hour.isin(range(8,14))][15:][:-14]
Out[108]:
x y
2010-01-01 08:15:00 NaN NaN
2010-01-01 08:16:00 NaN NaN
2010-01-01 08:17:00 NaN NaN
2010-01-01 08:18:00 NaN NaN
2010-01-01 08:19:00 NaN NaN
... ... ...
2010-01-04 13:41:00 NaN NaN
2010-01-04 13:42:00 NaN NaN
2010-01-04 13:43:00 NaN NaN
2010-01-04 13:44:00 NaN NaN
2010-01-04 13:45:00 NaN NaN
[1411 rows x 2 columns]
编辑: 彻底检查数据后,上面的索引并没有解决问题,因为数据仍然包含 2010-01-01 13:45:00
和 2010-01-02 08:15:00
之前:
In [147]: y[y.index.hour.isin(range(8,14))][15:][:-14].index[300:400]
Out[147]:
DatetimeIndex(['2010-01-01 13:15:00', '2010-01-01 13:16:00',
'2010-01-01 13:17:00', '2010-01-01 13:18:00',
'2010-01-01 13:19:00', '2010-01-01 13:20:00',
...
'2010-01-01 13:35:00', '2010-01-01 13:36:00',
'2010-01-01 13:37:00', '2010-01-01 13:38:00',
'2010-01-01 13:39:00', '2010-01-01 13:40:00',
'2010-01-01 13:41:00', '2010-01-01 13:42:00',
'2010-01-01 13:43:00', '2010-01-01 13:44:00',
'2010-01-01 13:45:00', '2010-01-01 13:46:00', # 13:46:00 should be excluded
'2010-01-01 13:47:00', '2010-01-01 13:48:00', # this should be excluded
'2010-01-01 13:49:00', '2010-01-01 13:50:00', # this should be excluded
'2010-01-01 13:51:00', '2010-01-01 13:52:00', # this should be excluded
'2010-01-01 13:53:00', '2010-01-01 13:54:00', # this should be excluded
'2010-01-01 13:55:00', '2010-01-01 13:56:00', # this should be excluded
'2010-01-01 13:57:00', '2010-01-01 13:58:00', # this should be excluded
'2010-01-01 13:59:00', '2010-01-02 08:00:00', # this should be excluded
'2010-01-02 08:01:00', '2010-01-02 08:02:00', # this should be excluded
'2010-01-02 08:03:00', '2010-01-02 08:04:00', # this should be excluded
'2010-01-02 08:05:00', '2010-01-02 08:06:00', # this should be excluded
'2010-01-02 08:07:00', '2010-01-02 08:08:00', # this should be excluded
'2010-01-02 08:09:00', '2010-01-02 08:10:00', # this should be excluded
'2010-01-02 08:11:00', '2010-01-02 08:12:00', # this should be excluded
'2010-01-02 08:13:00', '2010-01-02 08:14:00', # this should be excluded
'2010-01-02 08:15:00', '2010-01-02 08:16:00',
'2010-01-02 08:17:00', '2010-01-02 08:18:00',
'2010-01-02 08:19:00', '2010-01-02 08:20:00',
...
'2010-01-02 08:47:00', '2010-01-02 08:48:00',
'2010-01-02 08:49:00', '2010-01-02 08:50:00',
'2010-01-02 08:51:00', '2010-01-02 08:52:00',
'2010-01-02 08:53:00', '2010-01-02 08:54:00'],
dtype='datetime64[ns]', freq=None)
解决方法尝试
我尝试了多个 bool 掩码,但以下代码会将每个 0
截断为 14
AND 46
为 59
每小时的分钟:
y[(
y.index.hour.isin(range(8,14)) & y.index.minute.isin(range(15, 46))
)]
问题
必须有更好的方法以更有效的方式执行此操作,我可能会错过(或者 pandas
可能已经具有该功能)。使用 DateTimeIndex
切片数据的更精确/pythonic 方法是什么?例如:
y[(y.index.day("everyday") & y.index.time_between('08:15', '13:45'))]
甚至更好:
y[y.index("everyday 08:15 to 13:45")]
最佳答案
是的,此功能内置于 DataFrame.between_time
y.between_time("08:15", "13:45")
关于python - pandas - 使用 DateTimeIndex 切片 DataFrame 的 Pythonic 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52655536/