-2

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
Yael
  • 21
  • 1

2 Answers2

1

Found my answer here Issue checking for missing datetime value in series :

Use is pd.NaT instead of .isnull()

Yael
  • 21
  • 1
1

Just to complemente the right answers, in python comparing to None, False, and True can not be done with == operator, but with is operator:

a = True
a == True # False
a is True # True

The same applies for np.nan and pd.NaT

a = pd.NaT
a == pd.NaT # False
a is pd.NaT # True
jcaliz
  • 3,891
  • 2
  • 9
  • 13