Python Pandas - 從 PeriodIndex 物件返回頻率物件作為字串


要從 PeriodIndex 物件返回頻率物件作為字串,請使用 PeriodIndex.freqstr 屬性。

首先,匯入所需的庫 −

import pandas as pd

建立一個 PeriodIndex 物件。PeriodIndex 是一個不可變的 ndarray,它儲存指示時間中規律週期的序數 −

periodIndex = pd.PeriodIndex(['2021-09-25 07:50:35', '2019-10-30 04:35:45',
'2021-07-15 02:25:15', '2022-06-25 07:20:55'], freq="T")

顯示 PeriodIndex 物件 −

print("PeriodIndex...\n", periodIndex)

顯示 PeriodIndex 頻率為字串 −

print("\nPeriodIndex frequency object as a string...\n", periodIndex.freqstr)

示例

以下為程式碼 −

import pandas as pd

# Create a PeriodIndex object
# PeriodIndex is an immutable ndarray holding ordinal values indicating regular periods in time
# We have set the frequency using the "freq" parameter
periodIndex = pd.PeriodIndex(['2021-09-25 07:50:35', '2019-10-30 04:35:45',
'2021-07-15 02:25:15', '2022-06-25 07:20:55'], freq="T")

# Display PeriodIndex object
print("PeriodIndex...\n", periodIndex)

# Display PeriodIndex frequency
print("\nPeriodIndex frequency object...\n", periodIndex.freq)

# Display PeriodIndex frequency as string
print("\nPeriodIndex frequency object as a string...\n", periodIndex.freqstr)

輸出

產生以下程式碼 −

PeriodIndex...
PeriodIndex(['2021-09-25 07:50', '2019-10-30 04:35', '2021-07-15 02:25', '2022-06-25 07:20'],
dtype='period[T]')

PeriodIndex frequency object...
<Minute>

PeriodIndex frequency object as a string...
T

更新時間: 20-Oct-2021

151 瀏覽次數

開啟你的 職業生涯

透過完成課程來獲得認證

開始
廣告
© . All rights reserved.