問題描述
我正在嘗試將 UTC 時間轉換為本地時間.這是我之前的經歷
I am trying to convert utc time to local time. This is what I had before
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
我認為它做了正確的事情,但我不明白輸出格式.特別是
I think it did the right thing, but I dont understand the output format. In particular
1) 為什么轉換后的列顯示為新索引?
1) Why do the converted cols appear as the new index?
2) 我知道 -06:00(最后一行)是一個小時班次,所以時間是早上 6:00,我如何檢索該信息,準確的當地時間?
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?
所需的輸出,我想要發布的確切時間,包括與 UTC 的偏移量.當地時間 UTC 時間
Desired output, I want the exact time to be posted, including the offset from utc. local time utc time
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
:
Compare without .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到本地的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!