此问题使用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)
我尝试了多次布尔掩码,但是下面的代码将每小时每小时从0
到14
的截断时间分别为{strong> AND 和46
到59
分钟:
y[(
y.index.hour.isin(range(8,14)) & y.index.minute.isin(range(15, 46))
)]
必须有一种更好的方法,以一种我可能会错过的更有效的方式(或者pandas
已经具有该功能)。用DateTimeIndex
切片数据的更精确/ Python方法是什么?例如:
y[(y.index.day("everyday") & y.index.time_between('08:15', '13:45'))]
甚至更好:
y[y.index("everyday 08:15 to 13:45")]
答案 0 :(得分:4)
是的,此功能是DataFrame.between_time
内置的
y.between_time("08:15", "13:45")
答案 1 :(得分:3)
您几乎猜到了正确的函数名称。您可以使用函数DataFrame.between_time
来实现所需的过滤。
示例:
y_active = y.between_time('08:15', '13:45')