比较日期差异和熊猫时间戳值

时间:2019-03-26 17:00:00

标签: pandas python-2.7

我正在尝试根据数据帧中以天为单位的数据寿命执行特定的操作。我正在寻找的东西如下:

import pandas as pd

if 10days < (pd.Timestamp.now() - pd.Timestamp(2019, 3, 20)):
    print 'The data is older than 10 days' 

是否可以用两个时间戳值之间的差异替换“ 10days”或执行其他操作?

1 个答案:

答案 0 :(得分:0)

您要查找的是pd.Timedelta('10D')pd.Timedelta(10, unit='D')(或unit='days'unit='day')或pd.Timedelta(days=10)。例如,

In [37]: pd.Timedelta(days=10) < pd.Timestamp.now() - pd.Timestamp(2019, 3, 20)
Out[37]: False

In [38]: pd.Timedelta(days=5) < pd.Timestamp.now() - pd.Timestamp(2019, 3, 20)
Out[38]: True