How do i subtarct 2 time columns with each other in Python?
Question:
I have a column Start and HT where both are Object Datatype:
The output which is needed is (HT – Start) in minutes.
I try to convert them to datetime through pd.to_datetime but it throws error
TypeError: <class ‘datetime.time’> is not convertible to datetime
Start
HT
09:30:00
09:40:00
09:30:00
09:36:00
09:30:00
09:50:00
09:30:00
10:36:00
Expected Output
Start
HT
diff(in minutes)
09:30:00
09:40:00
10
09:30:00
09:36:00
6
09:30:00
09:50:00
20
09:30:00
10:36:00
66
Please help.
Answers:
You should fisrt convert dates using pd.to_datetime()
df['Start'] = pd.to_datetime(df['Start'], format='%H:%M:%S').dt.time.apply(str)
df['HT'] = pd.to_datetime(df['HT'], format='%H:%M:%S').dt.time.apply(str)
df['diff(in minutes)'] = (pd.to_timedelta(df['HT']) - pd.to_timedelta(df['Start'])).dt.total_seconds() / 60
print(df)
You can simplify the above code using pd.to_timedelta()
df['Start'] = pd.to_timedelta(df['Start'])
df['HT'] = pd.to_timedelta(df['HT'])
df['diff(in minutes)'] = (df['HT'] - df['Start']).dt.total_seconds() / 60
print(df)
Start HT diff(in minutes)
0 09:30:00 09:40:00 10.0
1 09:30:00 09:36:00 6.0
2 09:30:00 09:50:00 20.0
3 09:30:00 10:36:00 66.0
I have a column Start and HT where both are Object Datatype:
The output which is needed is (HT – Start) in minutes.
I try to convert them to datetime through pd.to_datetime but it throws error
TypeError: <class ‘datetime.time’> is not convertible to datetime
Start | HT |
---|---|
09:30:00 | 09:40:00 |
09:30:00 | 09:36:00 |
09:30:00 | 09:50:00 |
09:30:00 | 10:36:00 |
Expected Output
Start | HT | diff(in minutes) |
---|---|---|
09:30:00 | 09:40:00 | 10 |
09:30:00 | 09:36:00 | 6 |
09:30:00 | 09:50:00 | 20 |
09:30:00 | 10:36:00 | 66 |
Please help.
You should fisrt convert dates using pd.to_datetime()
df['Start'] = pd.to_datetime(df['Start'], format='%H:%M:%S').dt.time.apply(str)
df['HT'] = pd.to_datetime(df['HT'], format='%H:%M:%S').dt.time.apply(str)
df['diff(in minutes)'] = (pd.to_timedelta(df['HT']) - pd.to_timedelta(df['Start'])).dt.total_seconds() / 60
print(df)
You can simplify the above code using pd.to_timedelta()
df['Start'] = pd.to_timedelta(df['Start'])
df['HT'] = pd.to_timedelta(df['HT'])
df['diff(in minutes)'] = (df['HT'] - df['Start']).dt.total_seconds() / 60
print(df)
Start HT diff(in minutes)
0 09:30:00 09:40:00 10.0
1 09:30:00 09:36:00 6.0
2 09:30:00 09:50:00 20.0
3 09:30:00 10:36:00 66.0