Search code examples
pythonpandasdatetimetimedelta

Why doesn't python datetime give negative seconds and hours when subtracting two dates?


I would like to known why I do not get minus hours and minus seconds from datetime.timedelta?

I have the following method

def time_diff(external_datetime, internal_datetime):

    from pandas.core.indexes.period import parse_time_string # include for completeness 

    time_external = parse_time_string(external_datetime)[0]
    time_internal = parse_time_string(internal_datetime)[0]

    diff = time_external - time_internal
    return diff

all is as expected when the datetimes look like these;

external_datetime = "2020-01-01T00:00:00"
internal_datetime = "2020-01-02T00:00:00"

returned is a datetime.timedelta of -1 days (datetime.timedelta(days=-1))

why then when i change the times to;

external_datetime = "2020-01-01T00:00:00"
internal_datetime = "2020-01-01T01:30:00"

do I get a diff of datetime.timedelta(days=-1, seconds=81000)

I did wonder if it was due to being 'near' midnight but

external_datetime = "2020-01-01T11:00:00"
internal_datetime = "2020-01-01T11:30:00"

results in datetime.timedelta(days=-1, seconds=84600)

versions

  • python 3.8.2
  • pandas 1.1.4

Solution

  • From the documentation for timedelta:

    Only days, seconds and microseconds are stored internally. Arguments are converted to those units:

    • A millisecond is converted to 1000 microseconds.
    • A minute is converted to 60 seconds.
    • An hour is converted to 3600 seconds.
    • A week is converted to 7 days.

    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

    So the number of seconds and microseconds are guaranteed to be non-negative, with the number of days being positive or negative as necessary. It makes sense to let the larger unit days be positive or negative, as that will account for most of an arbitrary interval. days can be slightly more negative than necessary, with the smaller, limited units used to make up the difference.

    Note that with this representation, the sign of the interval is determined solely by the sign of the days.