简体   繁体   English

两个日期之间的差异产生不同的结果

[英]Difference between two dates yields different results

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. 2个日期之间的差大于90天,因此我期望2行都为“ false”。 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: 使用abs可以在差值为负数时小心:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM