I am trying to create a new column based on a condition of two column timestamps such as below,
def time_delta(df):
if df['a_time'] > df['b_time']:
res = 'Early'
else:
res = 'Late'
return res
or
df['new_col'] = np.where(df['a_time'] > df['b_time'], 'Early', 'Late')
But the a_time column sometimes has a NaN value, I want the result or value in the new column if the a_time is NaN to be NaN as well how can I do this or tweak what I have for this?
Desired Output
time_a | time_b | new_column
2019-08-19 22:25:26.133 2019-08-19 23:00:00.000 Before_b
NaN 2019-08-19 22:00:00.000 NaN
2019-08-19 23:00:00.000 2019-08-19 20:00:00.000 After_b
Thanks!
df['new_col'] = np.where(df['a_time'] > df['b_time'], 'Early', 'Late').mask(df['time_a'].isna())?