ในการส่งคืนอ็อบเจ็กต์ Period เป็นการประทับเวลาที่มีความถี่รายปี ให้ใช้ period.to_timestamp() เมธอดและตั้งค่าพารามิเตอร์ความถี่เป็น 'Y ’.
ขั้นแรก นำเข้าไลบรารีที่จำเป็น -
import pandas as pd
pandas.Period หมายถึงช่วงเวลาหนึ่ง การสร้างวัตถุรอบระยะเวลา
period = pd.Period(freq="S", year = 2021, month = 9, day = 18, hour = 17, minute = 20, second = 45)
แสดงวัตถุรอบระยะเวลา
print("Period...\n", period) ส่งคืนการแสดง Timestamp ของอ็อบเจ็กต์ Period เราได้ตั้งค่าความถี่โดยใช้พารามิเตอร์ "ความถี่" ความถี่ถูกกำหนดเป็น 'Y' เช่นทุกปี
print("\nPeriod to Timestamp with yearly (year-end) frequency...\n", period.to_timestamp(freq='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 = 17, minute = 20, second = 45)
# display the Period object
print("Period...\n", period)
# Return the Timestamp representation of the Period object
# We have set the frequency using the "freq" parameter
# The frequency is set as 'Y' i.e. yearly
print("\nPeriod to Timestamp with yearly (year-end) frequency...\n", period.to_timestamp(freq='Y')) ผลลัพธ์
ซึ่งจะได้รหัสดังต่อไปนี้
Period... 2021-09-18 17:20:45 Period to Timestamp with yearly (year-end) frequency... 2021-12-31 00:00:00