ในการจัดรูปแบบวัตถุจุด ให้ใช้ period.strftime() เมธอดและการแสดงปีโดยไม่ใช้ศตวรรษ ให้ตั้งค่าพารามิเตอร์เป็น %y
ขั้นแรก นำเข้าไลบรารีที่จำเป็น -
import pandas as pd
pandas.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