0

I have 2 panda datetime columns and I want to compare them as following:

#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 Answer 1

1

Try using lambda x: x.Date_Jan == pd.NaT or lambda x: x.Date_Jan.isnull().

Your current code is looking for a string called 'NaT' which is not what it actually is.

For February it would be Date_Feb != pd.NaT or Date_Feb.isnotnull()

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks @jhomr. What worked for my case was df['check_NaT_and_valid'] = df.apply(lambda x: pd.isnull(x.Date_Apr) and pd.notnull(x.Date_May),axis=1)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.