I have a dataframe with datetime timestamps (every 1 minute). I’d like to increase the time interval between rows to 5 minutes. Basically keep rows 0, 5, 10 etc and remove the rest. How would I do that?
Date Value 17/08/2017 04:00:00 0 17/08/2017 04:01:00 1 17/08/2017 04:02:00 2 17/08/2017 04:03:00 3 17/08/2017 04:04:00 4 17/08/2017 04:05:00 5 17/08/2017 04:06:00 6 17/08/2017 04:07:00 7 17/08/2017 04:08:00 8 17/08/2017 04:09:00 9 17/08/2017 04:10:00 10
Thanks
Advertisement
Answer
Firstly convert your date column to datetime dtype by using to_datetime()
method(If its already of datetime then ignore this step):
df['Date']=pd.to_datetime(df['Date'])
Finally You can do this by boolean masking:
newdf=df[df['Date'].dt.minute%5==0]
Now if you prints newdf
you will get your desired output:
Date Value 0 2017-08-17 04:00:00 0 5 2017-08-17 04:05:00 5 10 2017-08-17 04:10:00 10
If needed use reset_index()
method:
newdf=newdf.reset_index(drop=True)
Output of above code:
Date Value 0 2017-08-17 04:00:00 0 1 2017-08-17 04:05:00 5 2 2017-08-17 04:10:00 10