9

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 days +23:58:00')

Expected Output:

Timedelta('-0 days 00:02:00')

What is the correct way to calculate a negative time difference? Thank you!

Mykola Zotko
  • 15,583
  • 3
  • 71
  • 73

5 Answers5

6

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 import timedelta
d = 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

Haleemur Ali
  • 26,718
  • 5
  • 61
  • 85
4

We can do total_seconds

(pd.to_datetime('2021-05-21 06:00:00') - pd.to_datetime('2021-05-21 06:02:00')).total_seconds()
Out[9]: -120.0
BENY
  • 317,841
  • 20
  • 164
  • 234
  • Could you please explain the default behavior. – Mykola Zotko May 21 '21 at 13:41
  • I can imagine something like this happening: the timedelta object calculates a complete day back and then adds the number of hours:min:sec to that value. Which, technically is correct, but hard to look at. Just an observation. – Standard_101 May 21 '21 at 13:46
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')
Corralien
  • 109,409
  • 8
  • 28
  • 52
  • note that this is the incorrect delta, OP was curious how one might represent the delta as `0 days -00:02:00'` – Haleemur Ali May 21 '21 at 13:55
  • In this case you are loosing important information about negative time. If you compare for example which process last longer, you want to know that suddenly the time is lower than 0 – Adam Kuzański Aug 21 '23 at 19:07
2

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

Deepak
  • 470
  • 1
  • 3
  • 15
2

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')

#minutes
print(delta / np.timedelta64(1,'m')
-2.0
#seconds
delta / np.timedelta64(1,'s')
-120.0
Umar.H
  • 22,559
  • 7
  • 39
  • 74