pandas 熊猫时间从 UTC 到本地

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37814201/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 01:23:29  来源:igfitidea点击:

pandas time shift from utc to local

pandastimestamputc

提问by ganesh reddy

I am trying to convert utc time to local time. This is what I had before

我正在尝试将 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

This is what I did

这就是我所做的

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

I think it did the right thing, but I dont understand the output format. In particular

我认为它做了正确的事情,但我不明白输出格式。特别是

1) Why do the converted cols appear as the new index?

1) 为什么转换后的 cols 显示为新索引?

2) I understand that -06:00 (in the last row) is an hour shift, so the time is 6:00 am, how do I retrieve that information, the exact local time?

2)我知道 -06:00(在最后一行)是一个小时班,所以时间是早上 6:00,我如何检索该信息,准确的当地时间?

Desired output, I want the exact time to be posted, including the offset from utc. local time utc time

所需的输出,我想要发布确切的时间,包括与 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

回答by EdChum

When you call tz.localizeyou localize the index, if you want to modify the column you need to call dt.localizealso to add the timezone offset call dt.tz_convert('UTC'):

当您调用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]

Compare without .dt:

比较没有.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]