Filter Pandas DataFrame by time index

PythonPandas

Python Problem Overview


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.

Python Solutions


Solution 1 - Python

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

Solution 2 - Python

You can use query for a more concise option:

df.query("index < '2013-10-16 08:00:00'")

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser2113095View Question on Stackoverflow
Solution 1 - PythonKevin StoneView Answer on Stackoverflow
Solution 2 - PythonrachwaView Answer on Stackoverflow