Reputation: 63
I've got a dataframe with a column (t1) giving the time someone usually goes to bed and a column (t2) giving the time someone usually gets up, looking like that:
ID t1 t2
p001 22:30:00 7:00:00
...
p055 01:20:00 9:00:00
...
I want to create a new column giving the hours spent in bed, computed by those two times. A simple subtraction, like that
Df['hoursinbed'] = Df['t2']- Df['t1']
of course does not work since the starting time is usually on the day before.
I've also tried this:
t1 = pd.to_datetime(Df['t1'])
t2 = pd.to_datetime(Df['t2'])
print (pd.Timedelta(t2-t1).seconds / 60.0)
But probably this works only if the start- and end-time includes a date?
Does anyone have an idea how to solve this? TIA :)
Upvotes: 0
Views: 119
Reputation: 7594
You can do this, add one day to t2
if t1 > 12 hours
and then find the difference:
df['t1'] = pd.to_datetime(df['t1'])
df['t2'] = pd.to_datetime(df['t2'])
df['t2'] = np.where(df['t1'].dt.hour > 12, df['t2'] + pd.DateOffset(1), df['t2'])
df['timeinbed'] = df['t2'] - df['t1']
df['t1'] = df['t1'].dt.strftime('%H:%M')
df['t2'] = df['t2'].dt.strftime('%H:%M')
print(df)
ID t1 t2 timeinbed
0 p001 22:30 07:00 0 days 08:30:00
1 p055 01:20 09:00 0 days 07:40:00
Upvotes: 1