如何在pandas中的数据框中索引时间段?

时间:2017-10-09 18:20:03

标签: python pandas

我有一个Dataframe,其中包含日期时间作为索引和一些数据,如下所示:

date_index = pd.date_range('2015-12-01 00:00:00', freq='1H', periods=50)
df = pd.DataFrame(np.random.rand(50), index= date_index, columns=['Data'])

我想在所有日期内仅从06:0000:00的时间间隔中选择数据。我怎么能做到这一点?

我在考虑使用像df.at_time('6am')这样的东西,但这只返回这个特定小时的数据帧,而不是间隔。

我是大熊猫的新手,我无法找到如何做到这一点。

谢谢!

1 个答案:

答案 0 :(得分:3)

使用between_time

df.between_time('6:00:00', '23:59:59')
df.between_time('6:00:00', '23:59:59').index.hour.unique()
#Int64Index([6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22,
#            23],
#           dtype='int64')