Home > Back-end >  searching/filtering on negative days and time in column
searching/filtering on negative days and time in column

Time:10-25

I want to filter/search on negative dates/times in a column. I already filtered the difference between dates, dropped NaN and create frame via:

df = (df['date1'] - df['date2']).dropna().to_frame(name=None) 

Result of dataset

    0
0   8 days 00:00:00
1   8 days 00:00:00
2   43 days 00:00:00
3   -42 days 06:50:00
4   147 days 23:00:00

I tried this:

I tried using timedelta or searching on values but that did not work.

Expected result

    0
3   -42 days 06:50:00

Appreciate the help and effort. Thank you!

CodePudding user response:

Filter negative Timedeltas from subtracted Series:

s = df['date1'] - df['date2']

out = s[s < pd.Timedelta(0)].to_frame(0) 

CodePudding user response:

Filtering out the unwanted days beforehand can be more handy:

df = df.query('date1 > date2').eval('date1 - date2').dropna().to_frame(name=None) 
  • Related