- Jupyter Notebook Python 3.7
- Subtracting datetime one column from another
- df['dropoff_datetime']
- df['pickup_datetime']
- Getting error: unsupported operand types(s) for - 'str' and 'str'
- HELP!
Code:
import numpy as np df['trip_minutes'] = df['dropoff_datetime'] - df['pickup_datetime'] df['trip_minutes'] = df['trip_minutes']/np.timedelta64(1,'m')
Asked
Active
Viewed 109 times
0
-
Can you provide an example of the dataframe please. – PacketLoss Dec 30 '19 at 23:32
-
pickup_datetime dropoff_datetime 0 02/10/2015 08:46:15 AM 02/10/2015 08:59:50 AM – 2tan2ten Dec 30 '19 at 23:42
-
You can find a screenshot of dataframe at https://2tan2ten.com/df_datetime.png – 2tan2ten Dec 31 '19 at 00:07
1 Answers
0
You are encountering this error because you are trying to calculate the sum of two strings, which is not supported.
df
dropoff_datetime pickup_datetime
0 00:07:56 00:10:44
df['dropoff_datetime'] - df['pickup_datetime']
Traceback (most recent call last):
TypeError: unsupported operand type(s) for -: 'str' and 'str'
To calculate the time difference, you need to set the column in the dataframe to timedelta
. Then you will be able to perform your calculation;
df['dropoff_datetime'] = pd.to_timedelta(df['dropoff_datetime'])
df['pickup_datetime'] = pd.to_timedelta(df['pickup_datetime'])
df['dropoff_datetime'] - df['pickup_datetime']
0 -1 days +23:57:12
dtype: timedelta64[ns]
EDIT:
Given your updated df information, the above will not work with the data you specified. However, you can run the calculation below per line in the DF to retrieve the value;
trip_minutes = datetime.strptime(df['dropoff_datetime'][0], '%d-%m-%Y %H:%M:%S %p') - datetime.strptime(df['pickup_datetime'][0], '%d-%m-%Y %H:%M:%S %p')
datetime.timedelta(seconds=815)
This converts your strings to a datetime object as you calculate the difference.

PacketLoss
- 5,561
- 1
- 9
- 27