2017-05-09 65 views
2
df['first_timestamp'] = pd.to_datetime(df['first_timestamp']).dt.tz_localize('US/Central', ambiguous='infer') 
df['second_timestamp'] = pd.to_datetime(df['second_timestamp']).dt.tz_localize('US/Central', ambiguous='infer') 

AmbiguousTimeError:2014年11月2日1时02分14秒暧昧时错误大熊猫

这些时间戳是来自不包括时区csv文件。如果可能,我想在将它加载到我的数据库之前添加时区。有没有我做错了代码的模糊部分?我仍然收到错误。

回答

3

该日期时间在中央时间存在两次,例如,看到here

Nov 2, 2014 - Daylight Saving Time Ended
When local daylight time was about to reach
Sunday, November 2, 2014, 2:00:00 am clocks were turned backward 1 hour to
Sunday, November 2, 2014, 1:00:00 am local standard time instead
Sunrise and sunset was about 1 hour earlier on Nov 2, 2014 than the day before. There was more light in the morning.

如果这些实际上是UTC,你应该从UTC本地化:

In [11]: pd.Series([pd.Timestamp("2014-11-02 01:02:14 UTC")]).dt.tz_convert('US/Central') 
Out[11]: 
0 2014-11-01 20:02:14-05:00 
dtype: datetime64[ns, US/Central] 

或传真到了模棱两可的标志(而不是'infer')只选择一个:

In [12]: pd.Series([pd.Timestamp("2014-11-02 01:02:14")]).dt.tz_localize('US/Central', ambiguous=True) 
Out[12]: 
0 2014-11-02 01:02:14-05:00 
dtype: datetime64[ns, US/Central] 
+0

实际数据是(或应该)在中央时间,所以我无法从UTC本地化。通过设置True,这会为那些有问题的行添加时区?嗯,我想这可以工作 - 我怀疑这很重要。另外一个选项是我添加另一个名为'utc_offset'的'%z',但是ambiguous ='NaT',所以如果有问题,utc_offset列是空白的。 – trench

+1

@trench重点是在这些时候你不知道实际的时间(因为那个时候的时区_s_ ambigious),最有可能的最好的办法就是挑一个! :) –