Search code examples
pythonpandasdatetimetimezoneutc

Parse time from string in UTC format in a way comparable with the current time stamp


I would like to do create a pandas date range ranging from a parsed time to the current time, both in UTC. This is my best effort so far:

import dateutil
from datetime import datetime, timezone

start_time = dateutil.parser.isoparse ('2021-01-01T00Z')
end_time = datetime.now (timezone.utc)

Both timestamps are in the UTC timezone now, but creating the date range fails nevertheless:

> timestamps = pd.date_range (start=start_time, end=end_time, freq='H')
TypeError: Start and end cannot both be tz-aware with different timezones

Cause is that the tzinfo objects of both timestaps are UTC like, but of different types:

> print (start_time, end_time, type (start_time.tzinfo), type (end_time.tzinfo))
datetime.datetime(2020, 1, 1, 0, 0, tzinfo=tzutc()),
datetime.datetime(2021, 5, 13, 5, 45, 56, 866181, tzinfo=datetime.timezone.utc),
dateutil.tz.tz.tzutc,
datetime.timezone

So I'm stuck here. Is there a valid way to do this ? This could be either by parsing an ISO time format in UTC via 'datetime' or getting the current system time in UTC via dateutil. I did not find a solution for any of these.

Edit: Copy/paste error in the code examples


Solution

  • I'm not a pro but I made some research and found this solution using astimezone():

    import dateutil
    from datetime import datetime, timezone
    
    start_time = dateutil.parser.isoparse ('2021-01-01T00Z').astimezone(timezone.utc)
    end_time = datetime.now (timezone.utc)
    timestamps = pd.date_range (start=start_time, end=end_time, freq='H')
    timestamps
    

    which gives :

    DatetimeIndex(['2021-01-01 00:00:00+00:00', '2021-01-01 01:00:00+00:00',
                   '2021-01-01 02:00:00+00:00', '2021-01-01 03:00:00+00:00',
                   '2021-01-01 04:00:00+00:00', '2021-01-01 05:00:00+00:00',
                   '2021-01-01 06:00:00+00:00', '2021-01-01 07:00:00+00:00',
                   '2021-01-01 08:00:00+00:00', '2021-01-01 09:00:00+00:00',
                   ...
                   '2021-05-12 21:00:00+00:00', '2021-05-12 22:00:00+00:00',
                   '2021-05-12 23:00:00+00:00', '2021-05-13 00:00:00+00:00',
                   '2021-05-13 01:00:00+00:00', '2021-05-13 02:00:00+00:00',
                   '2021-05-13 03:00:00+00:00', '2021-05-13 04:00:00+00:00',
                   '2021-05-13 05:00:00+00:00', '2021-05-13 06:00:00+00:00'],
                  dtype='datetime64[ns, UTC]', length=3175, freq='H')