guilhermecgs
guilhermecgs

Reputation: 3041

Filtering dataframe based on elapsed time

I have a dataframe indexed by a Timestamp column.

2011-5-5 12:11               (data...)
2011-5-5 12:12               (data...)
2011-5-5 12:13               (data...)
2011-5-5 12:14               (data...)
2011-5-5 12:15               (data...)
2011-5-5 12:26               (data...)
2011-5-5 12:27               (data...)
2011-5-5 12:28               (data...)
2011-5-5 12:36               (data...)
2011-5-5 12:37               (data...)

I want to filter all rows that the time elapsed from the previous row is more than 1 minute

The result would be:

2011-5-5 12:11               (data...)
2011-5-5 12:12               (data...)
2011-5-5 12:13               (data...)
2011-5-5 12:14               (data...)
2011-5-5 12:15               (data...)
2011-5-5 12:26               (data...)   --- FILTERED (12:26 minus 12:15) = 11 minutes
2011-5-5 12:27               (data...)
2011-5-5 12:28               (data...)
2011-5-5 12:36               (data...)   --- FILTERED (12:36 minus 12:28) = 8 minutes
2011-5-5 12:37               (data...)

ps: I know that each time I execute the filtering process, more rows will be filtered

Upvotes: 0

Views: 98

Answers (1)

MaxU - stand with Ukraine
MaxU - stand with Ukraine

Reputation: 210832

try this:

In [36]: df[df.ts - df.ts.shift(1) > pd.Timedelta('1min')]
Out[36]:
                   ts
5 2011-05-05 12:26:00
8 2011-05-05 12:36:00

Upvotes: 4

Related Questions