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

3

我有一个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个回答

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')

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接