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

Rollback timestamp về offset CustomBusinessHour trước đó trong Pandas

Sử dụng CustomBusinessHour.rollback() để lùi timestamp về offset hợp lệ gần nhất trước đó (nếu timestamp không nằm trên offset).

Ví dụ

import pandas as pd

timestamp = pd.Timestamp('2021-12-20 08:35:10')
cbhOffset = pd.tseries.offsets.CustomBusinessHour(n=5, weekmask='Mon Tue Wed Fri')

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

roll_back = cbhOffset.rollback(pd.Timestamp('2021-12-18 08:35:10'))
print("Rollback result:", roll_back)

Kết quả

Timestamp: 2021-12-20 08:35:10
CustomBusinessHour Offset: <5 * CustomBusinessHours: CBH=09:00-17:00>
Updated Timestamp: 2021-12-20 14:00:00
Rollback result: 2021-12-17 17:00:00