Python Pandas - 将 naive Timestamp 转换为本地时区


要将 naive Timestamp 转换为本地时区,请使用timestamp.tz_locale()。其中,使用 tz 参数设置时区。

首先,导入需要的库 −

import pandas as pd

创建 naive timestamp

timestamp = pd.Timestamp('2021-09-14T15:12:34.261811624')

添加时区

timestamp.tz_localize(tz='Australia/Brisbane')

示例

以下列出了代码

import pandas as pd

# creating a naive timestamp
timestamp = pd.Timestamp('2021-09-14T15:12:34.261811624')

# display the Timestamp
print("Timestamp...\n", timestamp)

# add a timezone
print("\nTimestamp to local time zone...\n", timestamp.tz_localize(tz='Australia/Brisbane'))

输出

将生成以下代码

Timestamp...
 2021-09-14 15:12:34.261811624

Timestamp to local time zone...
 2021-09-14 15:12:34.261811624+10:00

更新日期:2021 年 10 月 13 日

4K+ 浏览量

开启你的 职业 生涯

通过完成课程获得认证

开始
广告
© . All rights reserved.