Python Pandas - 將給定的時間戳轉換為頻率為每小時的週期
要將給定的時間戳轉換為週期,請使用timestamp.to_period()方法。其中,使用freq引數設定頻率。對於每小時頻率,將 freq 設定為 H。
首先,匯入所需的庫 −
import pandas as pd
在 Pandas 中建立時間戳物件
timestamp = pd.Timestamp(2021, 9, 18, 11, 50, 20, 33)
將時間戳轉換為週期。我們已使用值“H”將“freq”引數設定了頻率為每小時
timestamp.to_period(freq='H')
示例
以下是程式碼
import pandas as pd
# set the timestamp object in Pandas
timestamp = pd.Timestamp(2021, 9, 18, 11, 50, 20, 33)
# display the Timestamp
print("Timestamp...\n", timestamp)
# convert timestamp to Period
# we have set the frequency as hourly using the "freq" parameter with value 'H'
print("\nTimestamp to Period with hourly frequency...\n", timestamp.to_period(freq='H'))輸出
這將生成以下程式碼
Timestamp... 2021-09-18 11:50:20.000033 Timestamp to Period with hourly frequency... 2021-09-18 11:00
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
JavaScript
PHP