Reputation: 15
How can I substract checkout_time
from purchase_time
to find total time spent on the website? Please view the DataFrame here: Table
I used the following code but it gives me an error. The format for time is 1/26/2017 14:44:
df['time_to_purchase'] = df.purchase_time - df.checkout_time
However I receive the following error:
TypeError: unsupported operand type(s) for -: 'float' and 'str'
Upvotes: 0
Views: 4458
Reputation: 40918
You'll need to convert the dtype of the columns to something that Pandas can recognize for doing datetime arithmetic:
fmt = '%m/%d/%Y %H:%M' # or: infer_datetime_format=True
df['purchase_time'] = pd.to_datetime(df['purchase_time'],
format=fmt,
errors='coerce')
df['checkout_time'] = pd.to_datetime(df['checkout_time'],
format=fmt,
errors='coerce')
Using errors='coerce'
in pd.to_datetime
will force unrecognized/unparseable dates to become NaT
("not a time").
Upvotes: 1