简体   繁体   English

熊猫:如何将UTC时间转换为当地时间?

[英]Pandas: How to Convert UTC Time to Local Time?

I have a Pandas series time of dates and times, like: 我有一个日期和时间的熊猫系列time ,例如:

UTC:  
 0   2015-01-01 00:00:00
1   2015-01-01 01:00:00
2   2015-01-01 02:00:00
3   2015-01-01 03:00:00
4   2015-01-01 04:00:00
Name: DT, dtype: datetime64[ns] 

That I'd like to convert to another timezone: 我想转换为另一个时区:

time2 = time.dt.tz_localize('UTC').dt.tz_convert('Europe/Rome')
print("CET: ",'\n', time2)

CET:  
0   2015-01-01 01:00:00+01:00
1   2015-01-01 02:00:00+01:00
2   2015-01-01 03:00:00+01:00
3   2015-01-01 04:00:00+01:00
4   2015-01-01 05:00:00+01:00
Name: DT, dtype: datetime64[ns, Europe/Rome]

But, the result is not what I need. 但是,结果不是我所需要的。 I want it in the form 2015-01-01 02:00:00 (the local time at UTC 01:00:00), not 2015-01-01 01:00:00+01:00 . 我想要的格式为2015-01-01 02:00:00 (当地时间为UTC 2015-01-01 02:00:00 ),而不是2015-01-01 02:00:00 2015-01-01 01:00:00+01:00

How can I do that? 我怎样才能做到这一点?

EDIT: While there is another question that deal with this issue ( Convert pandas timezone-aware DateTimeIndex to naive timestamp, but in certain timezone ), I think this question is more to the point, providing a clear and concise example for what appears a common problem. 编辑:虽然有另一个问题处理此问题( 将可识别熊猫时区的DateTimeIndex转换为朴素的时间戳,但在某些时区中 ),但我认为这个问题更重要,提供了一个简单明了的示例来说明常见的问题问题。

I turns out that my question already has an answer here: Convert pandas timezone-aware DateTimeIndex to naive timestamp, but in certain timezone ) 我发现我的问题已经在这里得到了答案: 将可识别熊猫时区的DateTimeIndex转换为朴素的时间戳,但是在特定的时区中

I just wasn't able to phrase my question correctly. 我只是无法正确表达我的问题。 Anyway, what works is: 无论如何,有效的方法是:

time3 = time2.dt.tz_localize(None)
print("Naive: ",'\n', time3)

Naive:  
 0   2015-01-01 01:00:00
1   2015-01-01 02:00:00
2   2015-01-01 03:00:00
3   2015-01-01 04:00:00
4   2015-01-01 05:00:00
Name: DT, dtype: datetime64[ns]`

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM