I have a pandas df with Time, Duration (in minutes, integer format), and Duration in timedelta format. The EndTime column has been parsed to time format.
id | EndTime | Duration_Minutes | Duration_TimeDelta |
---|---|---|---|
1 | 00:53:18 | 120 | 0 days 02:00:00 |
2 | 01:04:38 | 35 | 0 days 00:35:00 |
3 | 05:04:38 | 5 | 0 days 00:05:00 |
I wanted to created a new column: StartTime
I tried various things: from datetime import timedelta
df['StartTimeEstimate'] = df['EndTime'] - pd.Timedelta(hours=0, minutes=df['Minutes'], seconds=0)
or
df['StartTimeEstimate'] = df['EndTime'] - timedelta(minutes = df['Minutes'])
But couldn't quite work out a StartTime.
Ideally, I would like to see end StartTime to be: 22:53:18 12:29:38 04:59:38
You can't add time
and timedelta
, as the former doesn't have information about the date.
So if 'EndTime'
is of type time
, you can first convert it to str
and use pd.to_datetime
to convert it to datetime
. Then you can just subtract the timedelta
from the datetime
:
df["StartTimeEstimate"] = (
pd.to_datetime(df["EndTime"].astype(str)) - df["Duration_TimeDelta"]
).dt.time
id EndTime Duration_Minutes Duration_TimeDelta StartTimeEstimate
0 1 00:53:18 120 0 days 02:00:00 22:53:18
1 2 01:04:38 35 0 days 00:35:00 00:29:38
2 3 05:04:38 5 0 days 00:05:00 04:59:38
If 'EndTime'
is of type str
, you can remove the .astype(str)
.