Search code examples
pythonpandastime-seriestimestamptimedelta

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!


Solution

  • 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