parse time zone and convert to daylight saving time

被刻印的时光 ゝ 提交于 2020-08-09 07:22:48

问题


I have a pandas dataframe with a Datetime column:

         Datetime
0        2019-01-01 17:02:00
1        2019-01-01 17:03:00
2        2019-01-01 17:04:00
3        2019-01-01 17:05:00
...

The datetimes are in Eastern Standard Time (EST) WITHOUT Daylight savings adjustments (python doesn't know this). I need to convert the datetimes to US Central (Chicago) WITH Daylight savings adjustments. How can I do this, i.e.:

  1. tell python that the datetimes are in EST with no DST
  2. convert the datetimes to CT with DST

回答1:


Recap: you basically have datetime objects which are UTC-4 (EST), there is no transition to EDT (UTC-5).

What you could therefore do is localize from naive datetime to UTC by adding a timedelta of 4 hours and subsequently convert to CT:

import pandas as pd

# df with naive datetime objects that represent US/Eastern without DST
df = pd.DataFrame({'DateTime': pd.to_datetime(['2019-03-10 02:00:00',
                                               '2019-03-10 03:00:00',
                                               '2019-03-10 04:00:00'])})

# to UTC; EST is 4 hours behind UTC
df['DateTime_UTC'] = df['DateTime'].dt.tz_localize('UTC') + pd.Timedelta(hours=4)

# now convert from UTC to US/Central, UTC-6 with DST, -5 w/o DST
df['DateTime_CT'] = df['DateTime_UTC'].dt.tz_convert('US/Central')

# df['DateTime_CT']
# 0   2019-03-10 00:00:00-06:00
# 1   2019-03-10 01:00:00-06:00
# 2   2019-03-10 03:00:00-05:00
# Name: DateTime_CT, dtype: datetime64[ns, US/Central]

The example contains datetimes that would not exist with DST transition (2019-03-10 02:00:00). After the conversion to UTC to CT, DST transition is represented; 2019-03-10 01:00:00 -> 2019-03-10 03:00:00.




回答2:


You can first make it timezone aware by using tz_localize and then convert to Central time zone. Then you can check if it is daylight saving time by using dst. I added a later date as well. Once you know if it is dst, you can add or subtract 1 hour from it:

df['Datetime'] = pd.to_datetime(df['Datetime'])
df['New_Datetime'] = df['Datetime'].dt.tz_localize('US/Eastern').dt.tz_convert('US/Central')
df['is_dst'] = df['New_Datetime'].map(lambda x : int(x.dst().total_seconds()!=0))
print(df)

             Datetime              New_Datetime  is_dst
0 2019-01-01 17:02:00 2019-01-01 16:02:00-06:00       0
1 2019-01-01 17:03:00 2019-01-01 16:03:00-06:00       0
2 2019-01-01 17:04:00 2019-01-01 16:04:00-06:00       0
3 2019-01-01 17:05:00 2019-01-01 16:05:00-06:00       0
4 2019-06-06 17:05:00 2019-06-06 16:05:00-05:00       1


来源:https://stackoverflow.com/questions/62621797/parse-time-zone-and-convert-to-daylight-saving-time

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!