我正在尝试将utc时间转换为当地时间。这就是我之前的事情
df_combined_features['timestamp'][1:10]
2013-01-24 2013-01-24 11:00:00
2013-04-25 2013-04-25 10:00:00
2013-07-25 2013-07-25 10:00:00
2013-10-24 2013-10-24 10:00:00
2014-01-30 2014-01-30 11:00:00
2014-04-24 2014-04-24 10:00:00
2014-07-24 2014-07-24 10:00:00
2014-10-23 2014-10-23 10:00:00
2015-01-27 2015-01-27 11:00:00
这就是我做的事情
df_combined_features['time_stamp'].tz_localize('US/Central')[1:10]
2013-01-24 00:00:00-06:00 2013-01-24 11:00:00
2013-04-25 00:00:00-05:00 2013-04-25 10:00:00
2013-07-25 00:00:00-05:00 2013-07-25 10:00:00
2013-10-24 00:00:00-05:00 2013-10-24 10:00:00
2014-01-30 00:00:00-06:00 2014-01-30 11:00:00
2014-04-24 00:00:00-05:00 2014-04-24 10:00:00
2014-07-24 00:00:00-05:00 2014-07-24 10:00:00
2014-10-23 00:00:00-05:00 2014-10-23 10:00:00
2015-01-27 00:00:00-06:00 2015-01-27 11:00:00
我认为它做得对,但我不理解输出格式。特别是
1)为什么转换后的cols会显示为新索引?
2)据我所知-06:00(在最后一行)是一个小班,所以时间是早上6点,我该如何检索那些信息,确切的当地时间?
所需的输出,我想要发布确切的时间,包括从utc的偏移量。 当地时间utc时间
2013-01-24 05:00:00 2013-01-24 11:00:00
2013-04-25 05:00:00 2013-04-25 10:00:00
2013-07-25 05:00:00 2013-07-25 10:00:00
2013-10-24 05:00:00 2013-10-24 10:00:00
2014-01-30 05:00:00 2014-01-30 11:00:00
2014-04-24 05:00:00 2014-04-24 10:00:00
2014-07-24 05:00:00 2014-07-24 10:00:00
2014-10-23 05:00:00 2014-10-23 10:00:00
2015-01-27 05:00:00 2015-01-27 11:00:00
答案 0 :(得分:5)
当您致电tz.localize
时,您需要对索引进行本地化,如果您要修改列,则需要调用dt.localize
以添加时区偏移量调用dt.tz_convert('UTC')
:
In [125]:
df['timestamp'].dt.tz_localize('utc').dt.tz_convert('US/Central')
Out[125]:
index
2013-01-24 2013-01-24 05:00:00-06:00
2013-04-25 2013-04-25 05:00:00-05:00
2013-07-25 2013-07-25 05:00:00-05:00
2013-10-24 2013-10-24 05:00:00-05:00
2014-01-30 2014-01-30 05:00:00-06:00
2014-04-24 2014-04-24 05:00:00-05:00
2014-07-24 2014-07-24 05:00:00-05:00
2014-10-23 2014-10-23 05:00:00-05:00
2015-01-27 2015-01-27 05:00:00-06:00
Name: timestamp, dtype: datetime64[ns, US/Central]
在没有 .dt
的情况下进行比较:
In [126]:
df['timestamp'].tz_localize('utc').tz_convert('US/Central')
Out[126]:
index
2013-01-23 18:00:00-06:00 2013-01-24 11:00:00
2013-04-24 19:00:00-05:00 2013-04-25 10:00:00
2013-07-24 19:00:00-05:00 2013-07-25 10:00:00
2013-10-23 19:00:00-05:00 2013-10-24 10:00:00
2014-01-29 18:00:00-06:00 2014-01-30 11:00:00
2014-04-23 19:00:00-05:00 2014-04-24 10:00:00
2014-07-23 19:00:00-05:00 2014-07-24 10:00:00
2014-10-22 19:00:00-05:00 2014-10-23 10:00:00
2015-01-26 18:00:00-06:00 2015-01-27 11:00:00
Name: timestamp, dtype: datetime64[ns]