Python Pandas——将时间戳转换为其他时区


使用timestamp.tz_convert()将时间戳转换为其他时区。将时区设置为参数。首先,导入所需的库——

import pandas as pd

在 Pandas 中创建时间戳对象。我们还设置了时区

timestamp = pd.Timestamp('2021-10-14T15:12:34.261811624', tz='US/Eastern')

转换时间戳时区

timestamp.tz_convert('Australia/Brisbane'))

实例

以下是代码

import pandas as pd

# set the timestamp object in Pandas
# we have also set the timezone
timestamp = pd.Timestamp('2021-10-14T15:12:34.261811624', tz='US/Eastern')

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

# convert timezone
print("\nConvert the Timestamp timezone...\n",
timestamp.tz_convert('Australia/Brisbane'))

输出

这将生成以下代码

Timestamp...
 2021-10-14 15:12:34.261811624-04:00

Convert the Timestamp timezone...
 2021-10-15 05:12:34.261811624+10:00

更新日期: 2021 年 10 月 13 日

5 千次以上浏览

开启你的 职业

完成课程获得认证

开始
广告
© . All rights reserved.