Skip to content Skip to sidebar Skip to footer

Negative Time Difference In Pandas

I get this strange result by substracting earlier time stamp for later one: pd.to_datetime('2021-05-21 06:00:00') - pd.to_datetime('2021-05-21 06:02:00') Output: Timedelta('-1 day

Solution 1:

Timedelta('-1 days +23:58:00')is the proper representation of a negative time difference in pandas (and also in pure python)

# using pure python
from datetime import datetime
datetime(2021,5,21,6,0,0) - datetime(2021,5,21,6,2,0)
datetime.timedelta(days=-1, seconds=86280)

this is because the difference is properly calculated as -120 seconds, but individual time elements cannot exceed their moduli. the timedelta components are normalized. To represent negative 2 minutes, a negative day & positive time component are used.

from the python datetime module's documentation

and days, seconds and microseconds are then normalized so that the representation is unique, with

  • 0 <= microseconds < 1000000
  • 0 <= seconds < 3600*24 (the number of seconds in one day)
  • -999999999 <= days <= 999999999

Note that normalization of negative values may be surprising at first. For example:

from datetime importtimedeltad= timedelta(microseconds=-1)
(d.days, d.seconds, d.microseconds)
(-1, 86399, 999999)

it is possible to retrieve the total seconds as a negative integer using the method Timedelta.total_seconds

Solution 2:

We can do total_seconds

(pd.to_datetime('2021-05-2106:00:00') - pd.to_datetime('2021-05-2106:02:00')).total_seconds()
Out[9]: -120.0

Solution 3:

Use abs to get the time delta:

>>> abs(pd.to_datetime('2021-05-21 06:00:00') - pd.to_datetime('2021-05-21 06:02:00'))
Timedelta('0 days 00:02:00')

Solution 4:

Well your code is giving correct output ... Your result is Timedelta('-1 days +23:58:00') which is equal to -24:00:00 + 23:58:00 => 2 mins

Solution 5:

you can use np.timedelta64 to change the time delta to your desired output

as others have said, the pandas negative Timedelta object is the correct output in python.

import numpy as np
delta = pd.to_datetime('2021-05-21 06:00:00') - pd.to_datetime('2021-05-21 06:02:00')


print(delta)
Timedelta('-1 days +23:58:00')

#minutesprint(delta / np.timedelta64(1,'m')
-2.0#seconds
delta / np.timedelta64(1,'s')
-120.0

Post a Comment for "Negative Time Difference In Pandas"