user3276418
user3276418

Reputation: 1807

filter pandas dataframe for timedeltas

I got a pandas dataframe, containing timestamps 'expiration' and 'date'. I want to filter for rows with a certain maximum delta between expiration and date. When doing fr.expiration - fr.date I obtain timedelta values, but don't know how to get a filter criteria such as fr[timedelta(fr.expiration-fr.date)<=60days]

Upvotes: 3

Views: 2670

Answers (2)

user3276418
user3276418

Reputation: 1807

@ sashkello Thanks,

filterfr = filterfr[filterfr.expiration-filterfr.date <=  numpy.timedelta64(datetime.timedelta(days = 60))]

did the trick.

filterfr.expiration-filterfr.date 

resulted in timedelta64 values and raised TypeError: can't compare datetime.timedelta to long. Converting to numpy.timedelta before comparision worked.

Upvotes: 3

acushner
acushner

Reputation: 9946

for the 60 days you're looking to compare to, create a timedelta object of that value timedelta(days=60) and use that for the filter. and if you're already getting timedelta objects from the subtraction, recasting it to a timedelta seems unnecessary.

and finally, make sure you check the signs of the timedeltas you're comparing.

Upvotes: 3

Related Questions