解析时区并转换为夏令时
parse time zone and convert to daylight saving time
我有一个带有 Datetime
列的 pandas 数据框:
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
...
日期时间为东部标准时间 (EST),未进行夏令时调整(python 不知道这一点)。我需要通过夏令时调整将日期时间转换为美国中部(芝加哥)。我该怎么做,即:
- 告诉 python 日期时间是 EST,没有 DST
- 使用 DST 将日期时间转换为 CT
您可以先使用 tz_localize
使其识别时区,然后转换为中部时区。然后你可以使用 dst
检查是否是夏令时。我也添加了一个较晚的日期。一旦你知道它是否是夏令时,你可以从中增加或减少 1 小时:
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
回顾:你基本上有日期时间对象,它们是 UTC-4 (EST),没有转换到 EDT (UTC-5)。
因此,您可以做的是通过添加 4 小时的 timedelta 将原始日期时间本地化为 UTC,然后转换为 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]
该示例包含 DST 转换不存在的日期时间 (2019-03-10 02:00:00
)。 UTC转CT后,表示DST转换; 2019-03-10 01:00:00
-> 2019-03-10 03:00:00
.
我有一个带有 Datetime
列的 pandas 数据框:
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
...
日期时间为东部标准时间 (EST),未进行夏令时调整(python 不知道这一点)。我需要通过夏令时调整将日期时间转换为美国中部(芝加哥)。我该怎么做,即:
- 告诉 python 日期时间是 EST,没有 DST
- 使用 DST 将日期时间转换为 CT
您可以先使用 tz_localize
使其识别时区,然后转换为中部时区。然后你可以使用 dst
检查是否是夏令时。我也添加了一个较晚的日期。一旦你知道它是否是夏令时,你可以从中增加或减少 1 小时:
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
回顾:你基本上有日期时间对象,它们是 UTC-4 (EST),没有转换到 EDT (UTC-5)。
因此,您可以做的是通过添加 4 小时的 timedelta 将原始日期时间本地化为 UTC,然后转换为 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]
该示例包含 DST 转换不存在的日期时间 (2019-03-10 02:00:00
)。 UTC转CT后,表示DST转换; 2019-03-10 01:00:00
-> 2019-03-10 03:00:00
.