0

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
FObersteiner
  • 22,500
  • 8
  • 42
  • 72
Thomas Gardner
  • 33
  • 1
  • 10
  • why? because the `__str__` method of the timedelta class is coded like that? You can still write your own "strftime" for timedelta, see [Format timedelta to string](https://stackoverflow.com/q/538666/10197418) for inspiration. – FObersteiner May 27 '21 at 13:25
  • btw. what's the relation to pandas here, as far as I can tell, your question is specific to Python's `datetime.timedelta` (although that's not that different from pandas' timedelta). – FObersteiner May 27 '21 at 13:28
  • @MrFuppes I included pandas, as it is pandas that contains `parse_time_string` and I wasn't sure if it might be making a (small) difference. cheers for adding the other tag. – Thomas Gardner May 27 '21 at 14:01
  • no worries, as a side note: the format you have is nice ISO 8601, so you can parse conveniently with [datetime.fromisoformat](https://docs.python.org/3/library/datetime.html#datetime.datetime.fromisoformat). – FObersteiner May 27 '21 at 14:10

2 Answers2

2

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.

chepner
  • 497,756
  • 71
  • 530
  • 681
  • There is a gotcha with the restriction of positive values for seconds. Subtracting two `datetime` objects in one order will return `A - B ⇒ timedelta(seconds=…)`, but in reverse order `B - A ⇒ timedelta(days=-1, seconds=…)`. If you then apply those deltas to a date near DST change, you can get surprising results (because days don't have 24 hours when the daylight saving time starts or ends). Read: https://blog.ganssle.io/articles/2018/02/aware-datetime-arithmetic.html#python-s-datetime-semantics – Denilson Sá Maia Jan 13 '22 at 13:11
0

Time in computing is generally based on number of seconds (or some unit of seconds) past a reference time. I'd assume Python's DateTime represents data as hours, minutes, seconds, and subunits of seconds after the start of a day. Therefore, seconds will never be negative. Because it is always seconds after, it makes sense to go -1 day + 84600 seconds so that seconds are positive.

thshea
  • 1,048
  • 6
  • 18