gpt4 book ai didi

python - Pandas:将 pytz.FixedOffset 应用于系列

转载 作者:行者123 更新时间:2023-12-01 06:30:53 33 4
gpt4 key购买 nike

我有一个带有 timestamp 列的 DataFrame,如下所示:

0     2020-01-26 05:00:00-08:00
1 2020-01-26 06:00:00-08:00
[...]
Name: timestamp, dtype: datetime64[ns, pytz.FixedOffset(-480)]

(时间戳不是 DataFrame 的索引)

我希望将 pytz.FixedOffset(-480) 应用于(或者更确切地说未应用)到该列,以便它看起来像这样:

0     2020-01-26 13:00:00
1 2020-01-26 14:00:00
[...]

如何在不手动解析 timestamp 的情况下实现此目的?

最佳答案

使用Series.dt.tz_convert :

df['timestamp'] = df['timestamp'].dt.tz_convert(None)
print (df)
timestamp
0 2020-01-26 13:00:00
1 2020-01-26 14:00:00

关于python - Pandas:将 pytz.FixedOffset 应用于系列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59919877/

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