I am trying to write a function to compare a set of dates in each row of a dataframe after checking for NaT values in one of the columns. I get a "'Timestamp' object has no attribute" error on the isnull test.
import pandas as pd
a =['2018-01-01','2019-01-01','2020-01-01']
b =['2018-12-31','2019-12-31', 'NaT']
df = pd.DataFrame({"a":a,"b":b})
df = df.apply(pd.to_datetime)
#if date value in col b is later than col a return col b
def datefix(x):
if x['b'].isnull() == True:
result = x['a']
elif x['b']> x['a']:
result = x['b']
return result
#isnull test in function fails with error
df['c']=df.apply(datefix,axis=1)
What confuses me is when I use isnull over the same column in the dataframe direclty without a function the test works fine:
#isnull test works over dataframe
b = df[df['b'].isnull() == True]
print(b)
a b
2 2020-01-01 NaT