6

I am trying to create a new column 'Var' in the following Pandas DataFrame based on values from the other columns. I am encountering issues when dealing with the NaN, NaT.

Data: ( Used apply(pd.to_datetime) on the Date columns at a previous step)

Date C A Age
2017-12-13 1233.0 N 9
NaT NaN N 5
2007-09-24 49.0 N 14

Code:

def program(Flat):
    if Flat['A'] == 'N' :
        return 0
    elif Flat['Date'].isna() :
        return Flat['Age'] + 1
    elif Flat['C'] < 365 :
        return 1
    elif Flat['C'] >= 365 :
        return math.floor((Flat['C'])/365.25) + 1

Flat['Var'] = Flat.apply(program, axis=1)

Error: AttributeError: 'NaTType' object has no attribute 'isna'

Tried running through Anaconda & Python. All same error. Pandas version is 1.3.3.

What is the correct way to detect the NaT type?

merv
  • 67,214
  • 13
  • 180
  • 245
Martin
  • 73
  • 1
  • 1
  • 5

1 Answers1

3

"NaT" (for date/time types) and "NaN" are not the same. However, you can use the "isnull" function for both types:

    elif pd.isnull(Flat['Data']):
Tim Roberts
  • 48,973
  • 4
  • 21
  • 30
  • Thanks for the reply. I tried but still same problem. 'AttributeError: 'NaTType' object has no attribute 'isnull' – Martin Oct 15 '21 at 21:37
  • My mistake. It's a function to which you pass a value, not an attribute of the type. I fixed it. – Tim Roberts Oct 15 '21 at 21:38
  • Amazing! thanks! So the pd.isnull() reads the value through the dataframe Flat['Data'] in this case? – Martin Oct 15 '21 at 21:41
  • Well, `Flat['Data']` returns an object. It's either a datetime object or a NaTType object. The global `isnull` function can tell the difference between them. – Tim Roberts Oct 15 '21 at 22:28