Python Pandas - 將給定的 Period 物件的頻率從秒更改為小時頻率
要將給定 Period 物件的頻率從秒更改為小時頻率,請使用 period.asfreq() 方法並設定引數“H”。
首先,匯入所需的庫 -
import pandas as pd
pandas.Period 表示一段時間。建立 Period 物件。我們使用 'freq' 引數設定頻率為秒,即 'S'
period = pd.Period(freq="S", year = 2021, month = 4, day = 16, hour = 2, minute = 35, second = 15)
顯示具有秒頻率的 Period 物件
print("Period...\n", period)將 Period 從秒轉換為小時頻率。我們使用 asfreq() 將秒轉換為小時頻率,並將其設定為“H”
res = period.asfreq('H')
示例
以下是程式碼
import pandas as pd
# The pandas.Period represents a period of time
# Creating a Period object
# We have set the frequency as seconds ie. 'S' using the 'freq' parameter
period = pd.Period(freq="S", year = 2021, month = 4, day = 16, hour = 2, minute = 35, second = 15)
# display the Period object with Seconds frequency
print("Period...\n", period)
# Convert Period from Seconds to Hourly frequency
# We have set the "H" to convert seconds to hourly frequency using asfreq()
res = period.asfreq('H')
# display the result after conversion from Seconds to hourly frequency
print("\nFinal result after converting frequency ...\n", res)輸出
這會生成以下程式碼
Period... 2021-04-16 02:35:15 Final result after converting frequency ... 2021-04-16 02:00
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP