Python Pandas - 返回 Period 物件的時間戳表示形式
要返回 Period 物件的時間戳表示形式,請使用 period.to_timestamp() 方法。
首先,匯入所需的庫 −
import pandas as pd
pandas.Period 表示一段時間。建立一個 Period 物件
period = pd.Period(freq="S", year = 2021, month = 9, day = 18, hour = 17, minute = 20, second = 45)
顯示 Period 物件
print("Period...\n", period)
返回 Period 物件的時間戳表示形式。我們使用 “freq” 引數設定了頻率
print("\nPeriod to Timestamp...\n", period.to_timestamp(freq='T'))
示例
以下為程式碼
import pandas as pd # The pandas.Period represents a period of time # Creating a Period object period = pd.Period(freq="S", year = 2021, month = 9, day = 18, hour = 17, minute = 20, second = 45) # 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 print("\nPeriod to Timestamp...\n", period.to_timestamp(freq='T'))
輸出
這會生成以下程式碼
Period... 2021-09-18 17:20:45 Period to Timestamp... 2021-09-18 17:20:00
廣告