While calculating timedelta i get the wrong values, pandas
Question:
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
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
Answers:
You shouldn’t need to pass the difference to pd.Timedelta
and importantly use total_seconds
:
(s-s1).total_seconds()
output: -0.242
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
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
You shouldn’t need to pass the difference to pd.Timedelta
and importantly use total_seconds
:
(s-s1).total_seconds()
output: -0.242