我有一个timeseries文件,其中包含了具有夏令时的10年数据。时间是在当地时间的天真格式,地点是圣路易斯,美国,那里的多时区出现在一年。时间序列的一个样本如下:
local_time flow
11/3/12 23:30 58145400
11/4/12 0:00 58147200
11/4/12 0:30 58149000
11/4/12 1:00 58150800
11/4/12 1:30 58152600
11/4/12 1:00 58150800
11/4/12 1:30 58152600
11/4/12 2:00 58154400
11/4/12 2:30 58156200
11/4/12 3:00 58158000
11/4/12 3:30 58159800
11/4/12 4:00 58161600
11/4/12 4:30 58163400如果你仔细看完11/4/12 1:30 58152600之后,时间变成了11/4/12 1:00。今天是星期天,时钟倒计时一小时。
如果没有夏令,那么ts应该在下面看一下:
local_time flow
11/3/2012 23:30 58145400
11/4/12 0:00 58147200
11/4/12 0:30 58149000
11/4/12 1:00 58150800
11/4/12 1:30 58152600
11/4/12 2:30 58150800
11/4/12 3:00 58152600
11/4/12 3:30 58154400
11/4/12 4:00 58156200
11/4/12 4:30 58158000
11/4/12 5:30 58159800
11/4/12 6:00 58161600
11/4/12 6:30 58163400现在,在我的原始文件中有几个类似的实例。我想将本地数据转换为UTC或CST,在那里不会出现像本地时间序列数据那样的夏时制跳转。
我试过这个:
import pandas as pd
import numpy as np
df=pd.read_excel(r'test_dst.xlsx, sheet_name='Sheet1', header=0)
ts_naive=df.iloc[:,0]
ts_cst = ts_naive.dt.tz_localize('America/Chicago') # 'America/Chicago' uses CDT但是它给出了一个错误:AmbiguousTimeError: Cannot infer dst time from 2012-11-04 01:00:00, try using the 'ambiguous' argument
如果我使用以下内容,它会给出错误的输出:
ts_cst = ts_naive.dt.tz_localize('UTC').dt.tz_convert('America/Chicago')因为我将“UTC”时区分配给一个本地数据,这是错误的。
我的最终目标是从时刻表中删除夏时制时间跳转,这样我就可以在几秒钟内把它转换成一个不断增加的ts。我的模型只能在朱利安秒内花费时间,而时间序列只能增加。谢谢。下面是一个示例excel文件:dst.xlsx
发布于 2022-08-04 04:36:45
文档中有一个关于这一点的有用部分,特别是ambiguous="infer"参数~
df.local_time = pd.to_datetime(df.local_time)
df.local_time = df.local_time.dt.tz_localize('America/Chicago', 'infer')
print(df.local_time)
print(df.local_time.dt.tz_convert("UTC"))输出:
0 2012-11-03 23:30:00-05:00
1 2012-11-04 00:00:00-05:00
2 2012-11-04 00:30:00-05:00
3 2012-11-04 01:00:00-05:00
4 2012-11-04 01:30:00-05:00
5 2012-11-04 01:00:00-06:00
6 2012-11-04 01:30:00-06:00
7 2012-11-04 02:00:00-06:00
8 2012-11-04 02:30:00-06:00
9 2012-11-04 03:00:00-06:00
10 2012-11-04 03:30:00-06:00
11 2012-11-04 04:00:00-06:00
12 2012-11-04 04:30:00-06:00
Name: local_time, dtype: datetime64[ns, America/Chicago]
0 2012-11-04 04:30:00+00:00
1 2012-11-04 05:00:00+00:00
2 2012-11-04 05:30:00+00:00
3 2012-11-04 06:00:00+00:00
4 2012-11-04 06:30:00+00:00
5 2012-11-04 07:00:00+00:00
6 2012-11-04 07:30:00+00:00
7 2012-11-04 08:00:00+00:00
8 2012-11-04 08:30:00+00:00
9 2012-11-04 09:00:00+00:00
10 2012-11-04 09:30:00+00:00
11 2012-11-04 10:00:00+00:00
12 2012-11-04 10:30:00+00:00
Name: local_time, dtype: datetime64[ns, UTC]https://stackoverflow.com/questions/73229567
复制相似问题