Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Python

Kiểm tra timestamp có nằm trên offset CustomBusinessHour trong Pandas

Sử dụng CustomBusinessHour.is_on_offset() trong Pandas để kiểm tra xem một timestamp có rơi vào giờ làm việc tùy chỉnh hay không.

Ví dụ

import pandas as pd

timestamp = pd.Timestamp('2021-11-14 05:20:30')
cbhOffset = pd.tseries.offsets.CustomBusinessHour(start="09:30", end="18:30")

print("Timestamp:", timestamp)
print("CustomBusinessHour Offset:", cbhOffset)
print("Updated Timestamp:", timestamp + cbhOffset)

offset = cbhOffset.is_on_offset(pd.Timestamp('2021-11-20 05:20:30'))
print("Check if timestamp is on offset:", offset)

Kết quả

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