Để định dạng đối tượng Kỳ, hãy sử dụng period.strftime () và để hiển thị năm không có kỷ, hãy đặt tham số là% y.
Đầu tiên, hãy nhập các thư viện được yêu cầu -
import pandas as pd
Con gấu trúc.Period đại diện cho một khoảng thời gian. Tạo đối tượng Dấu chấm
period = pd.Period(freq="S", year = 2021, month = 9, day = 18, hour = 8, minute = 20, second = 45)
Hiển thị đối tượng Dấu chấm
print("Period...\n", period) Hiển thị kết quả. Ở đây, năm được hiển thị không có thế kỷ
print("\nString representation (year without century)...\n", period.strftime('%y'))
Ví dụ
Sau đây là mã
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')) Đầu ra
Điều này sẽ tạo ra mã sau
Period... 2021-09-18 08:20:45 String representation (year without century)... 21