today = pd.Timestamp.today()
for x in range(len(df)):
#trace back
if df.loc[x,'date_2022'] is pd.NaT and df.loc[x,'date_2021'] is not pd.NaT:
# extract month and day
d1 = today.strftime('%m-%d')
d2 = df.loc[x,'date_2021'].strftime('%m-%d')
# convert to datetime
d1 = datetime.strptime(d1, '%m-%d')
d2 = datetime.strptime(d2, '%m-%d')
# get difference in days
diff = d1 - d2
days = diff.days
#range 14 days
if days > 14:
df.loc[x,'inspection'] = 'check'
else:
df.loc[x,'inspection'] = np.nan
my aim is to add an inspection column, the condition is if the cell in 2022 is null(pd.NaT) but last year is not null, and it has past 14 days since the last year's date, how can I write it without using loop?