Python Pandas - 将朴素的时间戳转换为本地时区
要将原始时间戳转换为本地时区,请使用.在其中,使用tz参数设置时区。timestamp.tz_locale()
首先,导入所需的库-
import pandas as pd
创建一个简单的时间戳
timestamp = pd.Timestamp('2021-09-14T15:12:34.261811624')
添加时区
timestamp.tz_localize(tz='Australia/Brisbane')
示例
以下是代码
import pandas as pd #creatinganaivetimestamp timestamp = pd.Timestamp('2021-09-14T15:12:34.261811624') #displaytheTimestamp print("Timestamp...\n", timestamp) #addatimezone 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