我有多個 csv 檔案,我已將 DateTime 設定為索引。
df6.set_index("gmtime", inplace=True)
#correct the underscores in old datetime format
df6.index = [" ".join( str(val).split("_")) for val in df6.index]
df6.index = pd.to_datetime(df6.index)
時間是格林威治標準時間,但我認為當我為樹莓派設定時鐘時,它已保存為 BST(英國夏令時)。我想將時間向后移動一小時。當我使用
df6.tz_convert(pytz.timezone('utc'))
它給我以下錯誤,因為它假設時間是正確的。
無法轉換 tz-naive 時間戳,使用 tz_localize 進行本地化
如何將時間更改為一小時?
uj5u.com熱心網友回復:
給定包含日期/時間資訊作為字串的列,您將轉換為日期時間,本地化為時區(此處:歐洲/倫敦),然后轉換為 UTC。您可以在設定為索引之前執行此操作。
前任:
import pandas as pd
dti = pd.to_datetime(["2021-09-01"]).tz_localize("Europe/London").tz_convert("UTC")
print(dti) # notice 1 hour shift:
# DatetimeIndex(['2021-08-31 23:00:00 00:00'], dtype='datetime64[ns, UTC]', freq=None)
注意:設定時區意味著 DST 被考慮在內,即在這里,在冬天你會有 UTC 0 和在夏天 UTC 1。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/331248.html
下一篇:C#根據特定的天數將日期范圍分組