I have StartDate and ExitDate two columns in my dataframe with NaT values in ExitDate column
I wish to create a third column Tenure by finding Difference between ExitDate and StartDate.
StartDate ExitDate
2022-06-22 2022-08-07
2020-12-28 NaT
2020-12-09 2022-08-04
2019-05-28 2021-10-23
2022-04-27 NaT
The below code gives error
‘ufunc ‘isnat’ is only defined for np.datetime64 and np.timedelta64.’
Although the datatypes are
def calculateTenure(row):
if np.isnat(row.ExitDate):
ExitDate = pd.datetime.now()
return (row.ExitDate.year - row.StartDate.year)
df['Tenure'] = df.apply(lambda row:calculateTenure(row), axis=1)
The main motive is to check if ExitDate is NaT so that current date can be used for calculation instead.
1