Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting difference in two datetime columns in pandas dataframe [duplicate]

I have a dataframe and it shows the types as

signup_time       151112 non-null datetime64[ns]
purchase_time     151112 non-null datetime64[ns]

The actual values are in the format 2015-02-24 22:55:49

When I subtract two rows I get the difference in days like 52 days 03:51:22 how can I get the difference values in seconds

like image 378
John Constantine Avatar asked Dec 11 '25 14:12

John Constantine


1 Answers

1) Use .dt.total_seconds()

(df.purchase_time - df.signup_time).dt.total_seconds()

2) Or use np.timedelta64(1, 's')

(df.purchase_time - df.signup_time) / np.timedelta64(1, 's')

3) Or use .astype('timedelta64[s]')

(df.purchase_time - df.signup_time).astype('timedelta64[s]')
like image 82
Zero Avatar answered Dec 14 '25 03:12

Zero



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!