Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

Python Pandas - แสดงเวลาสิ้นสุดของชั่วโมงทำการกำหนดเอง แบบ 24 ชั่วโมง จาก BusinessHour Offset

เพื่อแสดงเวลาสิ้นสุดของชั่วโมงทำการ (business hour) ที่กำหนดเองในรูปแบบ 24 ชั่วโมง จากออบเจกต์ BusinessHour offset ให้ใช้คุณสมบัติ BusinessHour.end

ขั้นตอนตัวอย่าง

import pandas as pd

# สร้าง Timestamp
timestamp = pd.Timestamp('2021-9-30 06:50:20')
print("Timestamp...\n", timestamp)

# สร้าง BusinessHour Offset
# start = เวลาเริ่มต้นชั่วโมงทำการแบบ 24 ชม.
# end = เวลาสิ้นสุดชั่วโมงทำการแบบ 24 ชม.
# n = จำนวนชั่วโมงทำการ
bhOffset = pd.tseries.offsets.BusinessHour(start="09:30", end="18:00", n=8)

print("\nBusinessHour Offset...\n", bhOffset)
print("\nTimestamp ที่อัปเดต...\n", timestamp + bhOffset)
print("\nเวลาสิ้นสุดของชั่วโมงทำการกำหนดเอง...\n", bhOffset.end)

โค้ดเต็มและผลลัพธ์

import pandas as pd

timestamp = pd.Timestamp('2021-9-30 06:50:20')
print("Timestamp...\n", timestamp)

bhOffset = pd.tseries.offsets.BusinessHour(start="09:30", end="18:00", n=8)
print("\nBusinessHour Offset...\n", bhOffset)
print("\nTimestamp ที่อัปเดต...\n", timestamp + bhOffset)
print("\nเวลาสิ้นสุดของชั่วโมงทำการกำหนดเอง...\n", bhOffset.end)
Timestamp...
 2021-09-30 06:50:20
BusinessHour Offset...
 <8 * BusinessHours: BH=09:30-18:00>
Timestamp ที่อัปเดต...
 2021-09-30 17:30:00
เวลาสิ้นสุดของชั่วโมงทำการกำหนดเอง...
 (datetime.time(18, 0),)