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

Python Pandas CustomBusinessHour - ตรวจสอบว่า timestamp อยู่บน offset หรือไม่

ใช้ CustomBusinessHour.is_on_offset() ใน Pandas เพื่อตรวจสอบว่า timestamp ที่กำหนดอยู่บน offset หรือไม่ ส่ง timestamp เป็นอาร์กิวเมนต์

ขั้นตอน

  1. import pandas as pd
  2. สร้าง timestamp: pd.Timestamp('2021-11-14 05:20:30')
  3. สร้าง CustomBusinessHour offset: pd.tseries.offsets.CustomBusinessHour(start="09:30", end="18:30")
  4. เพิ่ม offset ให้ timestamp และแสดงผล
  5. ตรวจสอบด้วย cbhOffset.is_on_offset(pd.Timestamp('2021-11-20 05:20:30'))

ตัวอย่างโค้ด

import pandas as pd

timestamp = pd.Timestamp('2021-11-14 05:20:30')
print("Timestamp...\n", timestamp)

cbhOffset = pd.tseries.offsets.CustomBusinessHour(start="09:30", end="18:30")
print("\nCustomBusinessHour Offset...\n", cbhOffset)

print("\nUpdated Timestamp...\n", timestamp + cbhOffset)

offset = cbhOffset.is_on_offset(pd.Timestamp('2021-11-20 05:20:30'))
print("\nCheck if the given timestamp is on offset or not...\n", offset)

ผลลัพธ์

Timestamp... 2021-11-14 05:20:30
CustomBusinessHour Offset... 
Updated Timestamp... 2021-11-15 10:30:00
Check if the given timestamp is on offset or not... False