Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

While calculating timedelta i get the wrong values, pandas

So I have the following sample datetime value.
Originated by the following line of code and string

s = '2022-08-02T17:33:44.358Z'
s = pd.to_datetime(s)
# Print
Timestamp('2022-08-02 17:33:44.358000+0000', tz='UTC')

I also have a second string value converted to datetime

s1 = '2022-08-02T17:33:44.600Z'
s1 = pd.to_datetime(s1)
# Print
Timestamp('2022-08-02 17:33:44.600000+0000', tz='UTC')

When I calculate the timedelta I get a weird result somethin way beyond the amount of seconds that I should get

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

pd.Timedelta(s-s1).seconds
#This prints out
86399 seconds

Am I doing something wrong, I really don’t understand why there is so many seconds

>Solution :

You shouldn’t need to pass the difference to pd.Timedelta and importantly use total_seconds:

(s-s1).total_seconds()

output: -0.242

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading