简体   繁体   中英

Pandas: How to Convert UTC Time to Local Time?

I have a Pandas series time of dates and times, like:

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 .

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.

I turns out that my question already has an answer here: Convert pandas timezone-aware DateTimeIndex to naive timestamp, but in certain timezone )

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]`

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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