Nikita Gupta
Nikita Gupta

Reputation: 513

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.

Upvotes: 4

Views: 10950

Answers (3)

Nikita Gupta
Nikita Gupta

Reputation: 513

index_list= df.b[(df.b >= "2016-12-02 22:00:00") & (df.b <= "2016-12-03 00:00:00")].index.tolist()
df.drop(df.index[index_list] , inplace = True)

Upvotes: 1

Vaishali
Vaishali

Reputation: 38425

Convert the column b to datetime and then apply mask

df.b = pd.to_datetime(df.b, format = '%Y-%m-%d %H:%M:%S')
df[(df.b < '2016-12-02 22:00:00') | (df.b > '2016-12-03 00:00:00')]

    b                   a
3   2016-12-03 01:00:00 19.195610
4   2016-12-03 02:00:00 19.176413

Upvotes: 2

Tristan Reid
Tristan Reid

Reputation: 6154

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)]

Upvotes: 9

Related Questions