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?
CodePudding user response:
"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']):