简体   繁体   中英

How to subtract time in Pandas DataFrame

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'

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM