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.
Answers:
You want df.loc[df.index < '2013-10-16 08:00:00']
since you’re selecting by label (index) and not by value.
You can use query
for a more concise option:
df.query("index < '2013-10-16 08:00:00'")
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.
You want df.loc[df.index < '2013-10-16 08:00:00']
since you’re selecting by label (index) and not by value.
You can use query
for a more concise option:
df.query("index < '2013-10-16 08:00:00'")