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


要將給定 Period 物件的頻率從秒更改為天,請使用 period.asfreq() 方法並設定引數‘D’。

首先,匯入所需的庫 -

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() 將“D”設定為按天轉換

res = period.asfreq('D')

示例

以下是程式碼

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 Daily frequency
# We have set the "D" to convert seconds to daily frequency using asfreq()
res = period.asfreq('D')

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

輸出

這將生成以下程式碼

Period...
2021-04-16 02:35:15

Final result after converting frequency ...
2021-04-16

更新於: 2021 年 10 月 20 日

455 次瀏覽

開啟你的事業

完成課程獲得認證

開始
廣告
© . All rights reserved.