Pandas Python - 返回頻率為每天的時間戳作為週期物件


若要將週期物件作為時間戳返回,並以每日頻率顯示,請使用 period.to_timestamp() 方法,並將 freq 引數設定為“D”。

首先,匯入必要的庫 -

import pandas as pd

表示一段時間間隔的 pandas.Period。建立一個週期物件

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

顯示週期物件

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

返回週期物件的時間戳表示形式。我們已使用“freq”引數設定頻率。頻率設定為“D”,即每天

print("\nPeriod to Timestamp with daily frequency...\n", period.to_timestamp(freq='D'))

示例

以下為程式碼

import pandas as pd

# The pandas.Period represents a period of time
# Creating a Period object
period = pd.Period(freq="S", year = 2021, month = 11, day = 26, hour = 11, minute = 45, second = 55)

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

# Return the Timestamp representation of the Period object
# We have set the frequency using the "freq" parameter
# The frequency is set as 'D' i.e. daily
print("\nPeriod to Timestamp with daily frequency...\n", period.to_timestamp(freq='D'))

輸出

這將生成以下程式碼

Period...
2021-11-26 11:45:55

Period to Timestamp with daily frequency...
2021-11-26 00:00:00

更新於:2021-10-20

161 次瀏覽

開啟你的職業生涯

透過完成課程取得認證

開始
廣告