問題描述
我有一列(非索引列),其中包含日期時間.例如,前五個條目如下所示:
I have a column (non-index column) that has datetimes inside it. For example, the first five entries look something like this:
[Timestamp('2018-11-15 19:57:55'),
Timestamp('2018-11-15 19:59:46'),
Timestamp('2018-11-15 20:00:59'),
Timestamp('2018-11-15 20:01:41'),
Timestamp('2018-11-15 20:01:54')]
我想將條目從 UTC 轉換為太平洋時區.假設該列被稱為 times
我目前正在執行以下操作:
I want to convert the entries from UTC to the Pacific timezone. Assuming the column is called times
I am currently doing the following:
times.dt.tz_localize('GMT').dt.tz_convert('America/Los_Angeles')
雖然這成功地將列從 UTC 轉換為 PST,但輸出包含我不想要的無關組件.如下所示:
While this successfully converts the column from UTC to PST, the output has extraneous components that I do not want. It looks like the following:
[Timestamp('2018-11-15 11:57:55-0800', tz='America/Los_Angeles'),
Timestamp('2018-11-15 11:59:46-0800', tz='America/Los_Angeles'),
Timestamp('2018-11-15 12:00:59-0800', tz='America/Los_Angeles'),
Timestamp('2018-11-15 12:01:41-0800', tz='America/Los_Angeles'),
Timestamp('2018-11-15 12:01:54-0800', tz='America/Los_Angeles')]
如何從時間戳中刪除或忽略 -0800
?謝謝!
How do I remove or ignore the -0800
from the timestamps? Thanks!
推薦答案
只需添加最后一步.tz_localize(None)
:
import pandas as pd
d = pd.Series(['2018-11-15 19:57:55', '2018-11-15 19:59:46'])
d = pd.to_datetime(d)
d
0 2018-11-15 19:57:55
1 2018-11-15 19:59:46
dtype: datetime64[ns]
d_pacific_tz_aware = d.dt.tz_localize("GMT").dt.tz_convert('America/Los_Angeles')
d_pacific_tz_aware
0 2018-11-15 11:57:55-08:00
1 2018-11-15 11:59:46-08:00
dtype: datetime64[ns, America/Los_Angeles]
d_pacific_tz_naive = d.dt.tz_localize("GMT").dt.tz_convert('America/Los_Angeles').dt.tz_localize(None)
d_pacific_tz_naive
0 2018-11-15 11:57:55
1 2018-11-15 11:59:46
dtype: datetime64[ns]
這篇關于如何在 Pandas 中轉換 datetime 列的時區?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!