Python Pandas - 格式化週期物件並在不使用世紀的情況下顯示年份


要格式化週期物件,請使用 period.strftime() 方法,並且要在不使用世紀的情況下顯示年份,請將引數設定為 %y。

首先,匯入必要的庫 −

import pandas as pd

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

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

顯示週期物件

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

顯示結果。此處,年份以不使用世紀的方式顯示

print("\nString representation (year without century)...\n", period.strftime('%y'))

示例

以下是程式碼

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, year is displayed without century
print("\nString representation (year without century)...\n", period.strftime('%y'))

輸出

這將生成以下程式碼

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

String representation (year without century)...
21

更新日期:2021 年 10 月 20 日

211 次瀏覽

開啟您的事業

透過完成課程獲取證書

立即開始
廣告
© . All rights reserved.