Python Pandas - 將 Period 物件作為時間戳返回,粒度為分鐘


要將 Period 物件返回為頻率為每月的 timestamp,請使用 period.to_timestamp() 方法,並將 freq 引數設定為 'T'。

首先,匯入所需的庫 -

import pandas as pd

pandas.Period 表示一段時期。建立 Period 物件

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

顯示 Period 物件

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

返回 Period 物件的 Timestamp 表示。我們使用“freq”引數設定了頻率。頻率設定為 'T',即分鐘

print("\nPeriod to Timestamp with minutely frequency...\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 = 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 'T' i.e. monthly
print("\nPeriod to Timestamp with minutely frequency...\n", period.to_timestamp(freq='T'))

輸出

這將生成以下程式碼

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

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

已更新:2021 年 10 月 20 日

63 次瀏覽

開啟您的職業生涯

透過完成課程獲得認證

開始
廣告
© . All rights reserved.