Below is the example of a sample pandas dataframe and I am trying to find the difference between the dates in the two rows (with least dated row as the base - in this case second row). The difference between 2 dates is > than 90 days, hence I am expecting "false" for 2 rows. But for some reason, the result looks different.
PH_number date Type
H09879721 2018-10-29 AccountHolder
H09879731 2018-07-24 AccountHolder
Code:
print(df.date.diff()<=pd.Timedelta(90,'d'))
Current Result:
False
True
Expected Result:
False
False
Any suggestions would be appreciated.
Use abs
this takes care when difference is negative:
df.date.diff().abs().dt.days<=90
Or:
df.date.diff().abs().dt.days.le(90)
Or:
df.date.diff().abs()<=pd.Timedelta(90,'d')
Or:
df.date.diff().abs().le(pd.Timedelta(90,'d'))
0 False
1 False
Name: date, dtype: bool
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.