หากต้องการแสดงเวลาเริ่มต้นของชั่วโมงธุรกิจที่กำหนดเองในรูปแบบ 24 ชั่วโมงจากออบเจ็กต์ออฟเซ็ต CustomBusinessHour ให้ใช้คุณสมบัติ CustomBusinessHour.start ใน Pandas
ขั้นแรก นำเข้าไลบรารีที่จำเป็น -
import pandas as pd
ตั้งค่าอ็อบเจ็กต์ประทับเวลาใน Pandas -
timestamp = pd.Timestamp('2021-11-14 05:20:30')
สร้างออฟเซ็ต CustomBusinessHour ที่นี่ "เริ่มต้น" คือเวลาเริ่มต้นของเวลาทำการที่กำหนดเองของคุณในรูปแบบ 24 ชั่วโมง "สิ้นสุด" คือเวลาสิ้นสุดของเวลาทำการที่กำหนดเองของคุณในรูปแบบ 24 ชั่วโมง −
cbhOffset = pd.tseries.offsets.CustomBusinessHour(start="09:30", end = "18:00", n = 8)
เพิ่มออฟเซ็ตในการประทับเวลาและแสดงการประทับเวลาที่อัปเดต -
print("\nUpdated Timestamp...\n",timestamp + cbhOffset)
แสดงเวลาเริ่มต้นของเวลาทำการที่กำหนดเอง -
print("\nThe start time of the custom business hour...\n",cbhOffset.start) ตัวอย่าง
ต่อไปนี้เป็นรหัส -
import pandas as pd
# Set the timestamp object in Pandas
timestamp = pd.Timestamp('2021-11-14 05:20:30')
# Display the Timestamp
print("Timestamp...\n",timestamp)
# Create the CustomBusinessHour Offset
# CustomBusinessHour is the DateOffset subclass
# Here, "start" is the start time of your custom business hour in 24h format.
# The "end" is the end time of your custom business hour in 24h format.
cbhOffset = pd.tseries.offsets.CustomBusinessHour(start="09:30", end = "18:00", n = 8)
# Display the CustomBusinessHour Offset
print("\nCustomBusinessHour Offset...\n",cbhOffset)
# Add the offset to the Timestamp and display the Updated Timestamp
print("\nUpdated Timestamp...\n",timestamp + cbhOffset)
# Display the start time of the custom business hour
print("\nThe start time of the custom business hour...\n",cbhOffset.start) ผลลัพธ์
สิ่งนี้จะสร้างรหัสต่อไปนี้ -
Timestamp... 2021-11-14 05:20:30 CustomBusinessHour Offset... <8 * CustomBusinessHours: CBH=09:30-18:00> Updated Timestamp... 2021-11-15 17:30:00 The start time of the custom business hour... (datetime.time(9, 30),)