0

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

3
  • There is no df['Minutes'] in your df. Commented Jun 19, 2024 at 16:54
  • Is that supposed to be df['Duration_Minutes']? Commented Jun 19, 2024 at 16:56
  • yes, it was a typo. Thanks for spotting it! I corrected it in the question now. Commented Jun 20, 2024 at 7:41

1 Answer 1

2

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).

Sign up to request clarification or add additional context in comments.

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.