0
  1. Jupyter Notebook Python 3.7
  2. Subtracting datetime one column from another
  3. df['dropoff_datetime']
  4. df['pickup_datetime']
  5. Getting error: unsupported operand types(s) for - 'str' and 'str'
  6. HELP!
  7. Code:

    import numpy as np
    df['trip_minutes'] = df['dropoff_datetime'] - df['pickup_datetime']
    df['trip_minutes'] = df['trip_minutes']/np.timedelta64(1,'m')
    
hpaulj
  • 221,503
  • 14
  • 230
  • 353
2tan2ten
  • 109
  • 1
  • 9

1 Answers1

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