Mykola Zotko
Mykola Zotko

Reputation: 17794

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 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!

Upvotes: 9

Views: 4444

Answers (5)

Umar.H
Umar.H

Reputation: 23099

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

Upvotes: 2

Deepak
Deepak

Reputation: 470

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

Upvotes: 2

Haleemur Ali
Haleemur Ali

Reputation: 28233

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

Upvotes: 6

Corralien
Corralien

Reputation: 120401

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

Upvotes: 3

BENY
BENY

Reputation: 323226

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

Upvotes: 5

Related Questions