在Pandas时间序列中每天查询相同的时间值

Tho*_*wne 5 python datetime dataframe pandas

我想每天获得07h00值,来自每天有24小时分钟数据的多日数据框架.

import numpy as np
import pandas as pd

aframe = pd.DataFrame([np.arange(10000), np.arange(10000) * 2]).T
aframe.index = pd.date_range("2015-09-01", periods = 10000, freq = "1min")

aframe.head()
Out[174]: 
                     0  1
2015-09-01 00:00:00  0  0
2015-09-01 00:01:00  1  2
2015-09-01 00:02:00  2  4
2015-09-01 00:03:00  3  6
2015-09-01 00:04:00  4  8

aframe.tail()
Out[175]: 
                        0      1
2015-09-07 22:35:00  9995  19990
2015-09-07 22:36:00  9996  19992
2015-09-07 22:37:00  9997  19994
2015-09-07 22:38:00  9998  19996
2015-09-07 22:39:00  9999  19998
Run Code Online (Sandbox Code Playgroud)

在这10万行DataFrame中,我将如何在7天内尽可能高效地获取每天7am的价值?假设我可能必须为非常大的tick数据库执行此操作,因此我非常重视速度和低内存使用率.

我知道我可以用字符串索引,例如:

aframe.ix["2015-09-02 07:00:00"]
Out[176]: 
0    1860
1    3720
Name: 2015-09-02 07:00:00, dtype: int64
Run Code Online (Sandbox Code Playgroud)

但我需要的基本上是通配符样式查询

aframe.ix["* 07:00:00"]
Run Code Online (Sandbox Code Playgroud)

Ale*_*ley 7

你可以使用indexer_at_time:

>>> locs = aframe.index.indexer_at_time('7:00:00')
>>> aframe.iloc[locs]
                        0      1
2015-09-01 07:00:00   420    840
2015-09-02 07:00:00  1860   3720
2015-09-03 07:00:00  3300   6600
2015-09-04 07:00:00  4740   9480
2015-09-05 07:00:00  6180  12360
2015-09-06 07:00:00  7620  15240
2015-09-07 07:00:00  9060  18120
Run Code Online (Sandbox Code Playgroud)

还有indexer_between_time,如果你需要选择摆在一天中的两个特定时间之间的所有指标.

这两种方法都返回所需值的整数位置; iloc如上所示,可以使用相应的Series或DataFrame行获取.