Filter Pandas DataFrame by time index

Question:

I have a pandas DataFrame from 6:36 AM to 5:31 PM. I want to remove all observations where the time is less than 8:00:00 AM. Here is my attempt:

df = df[df.index < '2013-10-16 08:00:00']

This does nothing, please help.

Asked By: user2113095

||

Answers:

You want df.loc[df.index < '2013-10-16 08:00:00'] since you’re selecting by label (index) and not by value.

selecting by label

Answered By: Kevin Stone

You can use query for a more concise option:

df.query("index < '2013-10-16 08:00:00'")
Answered By: rachwa
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.