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 #setthetimestampobjectinPandas #wehavealsosetthetimezone timestamp = pd.Timestamp('2021-10-14T15:12:34.261811624', tz='US/Eastern') #displaytheTimestamp print("Timestamp...\n", timestamp) #converttimezone 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