Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove all rows between two timestamps from pandas dataframe

How to delete all rows from dataframe between two timestamps inclusive?

my Dataframe looks like :

                   b      a      
0 2016-12-02 22:00:00  19.218519 
1 2016-12-02 23:00:00  19.171197  
2 2016-12-03 00:00:00  19.257836  
3 2016-12-03 01:00:00  19.195610  
4 2016-12-03 02:00:00  19.176413 

For eg : I want to delete all rows from above dataframe whose timestamp falls is in between : "2016-12-02 22:00:00" to "2016-12-03 00:00:00". So, the result will contain only rows 3 and 4.

the type of b column is datetime64 and the type of a is float.

Please suggest.

like image 658
Nikita Gupta Avatar asked Aug 31 '25 05:08

Nikita Gupta


1 Answers

You can filter those out:

from_ts = '2016-12-02 22:00:00'
to_ts = '2016-12-03 00:00:00'
df = df[(df['b'] < from_ts) | (df['b'] > to_ts)]
like image 151
Tristan Reid Avatar answered Sep 02 '25 17:09

Tristan Reid