Python Pandas - 格式化 Period 物件並顯示季度


若要格式化 Period 物件,請使用 period.strftime() 方法並設定引數為 Q%q 以顯示季度。

首先,匯入必需的庫 −

import pandas as pd

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

period = pd.Period(freq="S", year = 2021, month = 9, day = 18, hour = 8, minute = 20, second = 45)

顯示 Period 物件

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

顯示結果。此處,Period 物件已格式化,季度也已顯示

print("\nString representation (display quarter)...\n", period.strftime('Q%q'))

示例

以下為該程式碼

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 = 8, minute = 20, second = 45)

# display the Period object
print("Period...\n", period)

# display the result
# Here, Period object is formatted and Quarter is displayed
print("\nString representation (display quarter)...\n", period.strftime('Q%q'))

輸出

這將生成以下程式碼

Period...
2021-09-18 08:20:45

String representation (display quarter)...
Q3

更新時間:20-Oct-2021

2K+ 瀏覽量

啟動你的 職業

完成課程以獲得認證

開始
廣告
© . All rights reserved.