gpt4 book ai didi

Pandas 时间从UTC到本地

转载 作者:行者123 更新时间:2023-12-04 18:03:24 25 4
gpt4 key购买 nike

我正在尝试将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)为什么转换后的列显示为新索引?

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

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

最佳答案

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

关于 Pandas 时间从UTC到本地,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37814201/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com