Claudio Pierard
Claudio Pierard

Reputation: 179

How can I index a time period in a dataframe in pandas?

I have a Dataframe with datetimes as indexes and some data, like this:

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

I would like to select the data only from the time interval 06:00 to 00:00 for all the days. How can I achive this?

I was thinking in using something like df.at_time('6am'), but this only returns the dataframe of this specific hour, not from the interval.

I am new to pandas and I can't find how to do this.

Thanks!

Upvotes: 3

Views: 313

Answers (1)

akuiper
akuiper

Reputation: 215057

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

Upvotes: 3

Related Questions