'Preserve timezone for pandas.Series.dt.time
I have a list of string representation of timezone-aware times, such as ["15:44:41.012955-04:00", "16:24:41.123456-05:00"], where the timezones are not guaranteed to be the same for all values.
I want to convert this to a Pandas series of timezone-aware datetime.time objects.
However, there are a few issues:
pd.to_datetime(...).dt.time() does not work for mixed types. For example, when using a list of time strings can include None or times of varying timezones, pd.to_datetime() will convert it to a series with dtype object (instead of datetime64) where the underlying values are pd.Timestamp().
# The timezones are different
> ser = pd.to_datetime(pd.Series(["15:44:41.012955-04:00", "16:24:41.123456-05:00"]), format="%H:%M:%S.%f%z")
0 1900-01-01 15:44:41.012955-04:00
1 1900-01-01 16:24:41.123456-05:00
dtype: object
> ser.dt.time
AttributeError: Can only use .dt accessor with datetimelike values
The other issue is datetime.time objects in the series produced by pd.to_datetime(...).dt.time do not have the timezone.
> ser = pd.to_datetime(pd.Series(["15:44:41.012955-10:00", "16:24:41.123456-10:00"]), format="%H:%M:%S.%f%z")
0 1900-01-01 15:44:41.012955-10:00
1 1900-01-01 16:24:41.123456-10:00
dtype: datetime64[ns, pytz.FixedOffset(-600)]
> ser.dt.time
0 15:44:41.012955
1 16:24:41.123456
dtype: object
How can I get a series of timezone-aware datetime.time objects when passing in a list of string times where the times can have mixed timezones or be None.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
