Reputation: 113
I have a dataframe with start and end dates. I am trying to create a third column with the following conditions:
I have been able to create a column with a 24 hour difference, but I am not able to create a loc-statement that can do the above. Any help?
df2['end_shutdown_analysis'] = df2['Shutdown timestamp'] + timedelta(hours=24)
Upvotes: 0
Views: 58
Reputation: 24314
you can try via np.where()
:
import numpy as np
df2['end_shutdown_analysis'] =np.where(
df2['Shutdown timestamp'].dt.hour<24, # condition
df['start']-df['end'], # value if true
df2['Start']+pd.DateOffset(hours=24) # else value.
)
OR
via loc
:
m=df2['Shutdown timestamp'].dt.hour<24
df.loc[m,'end_shutdown_analysis']=df['start']-df['end']
df.loc[~m,'end_shutdown_analysis']=df2['start']+pd.DateOffset(hours=24)
Note: you can also use pd.Timedelta(hours=24)
in place of pd.DateOffset(hours=24)
Upvotes: 2