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.