0

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!

3
  • 1
    hi, please provide a minimal reproducible example Commented Sep 4, 2019 at 21:28
  • @Yuca Thanks!, I added desired output Commented Sep 4, 2019 at 21:57
  • 1
    df['new_col'] = np.where(df['a_time'] > df['b_time'], 'Early', 'Late').mask(df['time_a'].isna()) ? Commented Sep 4, 2019 at 21:59

2 Answers 2

2

You could use numpy.isnat to fix it.

df['new_col'] = np.where(df['a_time'] > df['b_time'], 'Early', 'Late')

df.loc[(np.isnat(df['a_time']) | np.isnat(df['b_time'])), 'new_col'] = np.NaN
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the response, so run the code I have then run the code you showed in the next line below?
1

You may do it with np.select

df['new_column'] = np.select([df.isna().any(1), df.time_a > df.time_b], [pd.NaT, 'Early'], 'Late')

Out[923]:
                   time_a              time_b new_column
0 2019-08-19 22:25:26.133 2019-08-19 23:00:00  Late
1 NaT                     2019-08-19 22:00:00  NaT
2 2019-08-19 23:00:00.000 2019-08-19 20:00:00  Early

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.