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
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP