Python Pandas - 將給定 Period 物件的頻率從秒更改為分鐘頻率


要將給定 Period 物件的頻率從秒更改為分鐘頻率,請使用 **period.asfreq()** 方法並設定引數 **'T'**。

首先,匯入所需的庫 -

import pandas as pd

pandas.Period 表示一段時間。建立 Period 物件。我們使用 'freq' 引數將頻率設定為秒,即 'S'

period = pd.Period(freq="S", year = 2021, month = 9, day = 11, hour = 8, minute = 20, second = 45)

顯示具有秒頻率的 Period 物件

print("Period...\n", period)

將 Period 從秒轉換為分鐘頻率。我們使用 asfreq() 設定 "T" 將秒轉換為分鐘頻率

res = period.asfreq('T')

示例

以下是程式碼

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 = 9, day = 11, hour = 8, minute = 20, second = 45)

# display the Period object with Seconds frequency
print("Period...\n", period)

# Convert Period from Seconds to Minutely frequency
# We have set the "T" to convert seconds to minutely frequency using asfreq()
res = period.asfreq('T')

# display the result after conversion from Seconds to Minutely frequency
print("\nFinal result after converting frequency ...\n", res)

輸出

這將生成以下程式碼

Period...
2021-09-11 08:20:45

Final result after converting frequency ...
2021-09-11 08:20

更新於: 2021年10月20日

142 次檢視

開啟您的 職業生涯

透過完成課程獲得認證

開始
廣告

© . All rights reserved.