比较python熊猫中的日期

时间:2018-10-05 18:22:01

标签: python pandas datetime

我有2个熊猫datetime列,我想将它们进行如下比较:

#check if the Date_Jan precedes Date_Feb
df['find_preceding'] = df.apply(lambda x: x.Date_Jan < x.Date_Feb, axis =1) # works 

# check if the Date_Jan follows Date_Feb
df['find_following'] = df.apply(lambda x: x.Date_Jan > x.Date_Feb, axis =1) # works

# check if Date_Jan and Date_Feb are the same    
df['find_same'] = df.apply(lambda x: x.Date_Jan == x.Date_Feb, axis =1) # works 

# check if Date_Jan is NaT and Date_Feb is a date
df['check_NaT_and_valid'] = df.apply(lambda x: x.Date_Jan == 'NaT' and x.Date_Feb != 'NaT', axis =1) # NOT working

1 个答案:

答案 0 :(得分:1)

尝试使用lambda x: x.Date_Jan == pd.NaTlambda x: x.Date_Jan.isnull()

您当前的代码正在寻找一个名为“ NaT”的字符串,而不是实际的字符串。

2月为Date_Feb != pd.NaTDate_Feb.isnotnull()