Python Pandas - 使用分鐘頻率將給定的時間戳轉換為週期


若要將給定的時間戳轉換為週期,請使用 timestamp.to_period() 方法。在其中,使用 freq 引數設定頻率。對於分鐘頻率,將 freq 設定為 T。

首先,匯入所需的庫 -

import pandas as pd

建立一個時間戳物件

timestamp = pd.Timestamp(2021, 9, 6, 11, 50, 20, 33)

將時間戳轉換為週期。我們使用帶有值“T”的“freq”引數將頻率設定為分鐘

timestamp.to_period(freq='T')

示例

以下是程式碼

import pandas as pd

# set the timestamp object in Pandas
timestamp = pd.Timestamp(2021, 9, 6, 11, 50, 20, 33)

# display the Timestamp
print("Timestamp...\n", timestamp)

# convert timestamp to Period
# we have set the frequency as minutely using the "freq" parameter with value 'T'
print("\nTimestamp to Period...\n", timestamp.to_period(freq='T'))

輸出

將生成以下程式碼

Timestamp...
 2021-09-06 11:50:20.000033
Timestamp to Period...
 2021-09-06 11:50

更新於: 2021 年 10 月 13 日

133 次瀏覽

開始您的 職業生涯

透過完成課程獲得認證

開始
廣告
© . All rights reserved.