Python Pandas - 將週期物件作為具有月頻的時間戳返回
若要將週期物件作為具有月頻的時間戳返回,請使用 period.to_timestamp() 方法,並將 freq 引數設為“M”。
首先,匯入所需的庫 −
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”引數設定頻率。頻率設定為“M”,即每月
print("\nPeriod to Timestamp with monthly (month-end) frequency...\n", period.to_timestamp(freq='M'))
示例
以下是程式碼
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 # The frequency is set as 'M' i.e. monthly print("\nPeriod to Timestamp with monthly (month-end) frequency...\n", period.to_timestamp(freq='M'))
輸出
這將生成以下程式碼
Period... 2021-09-18 17:20:45 Period to Timestamp with monthly (month-end) frequency... 2021-09-30 00:00:00
廣告