Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filter Pandas DataFrame by time index

Tags:

python

pandas

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.

like image 946
user2113095 Avatar asked Nov 27 '13 02:11

user2113095


People also ask

How do you filter data frames based on time?

between_time() is a Pandas DataFrame method that filters for rows in a Pandas DataFrame between a start and end time. The parameters are: start_time : datetime. time or str.

How do you sort a DataFrame by index?

To sort a Pandas DataFrame by index, you can use DataFrame. sort_index() method. To specify whether the method has to sort the DataFrame in ascending or descending order of index, you can set the named boolean argument ascending to True or False respectively. When the index is sorted, respective rows are rearranged.


1 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

like image 84
Kevin Stone Avatar answered Sep 30 '22 13:09

Kevin Stone