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

Python Pandas: Trả về mã quy tắc (rule code) trên BusinessHour object

Thuộc tính BusinessHour.rule_code trong Pandas trả về mã quy tắc tần suất (frequency rule code) được áp dụng trên đối tượng BusinessHour offset.

Các bước thực hiện

1. Import thư viện

import pandas as pd

2. Thiết lập timestamp

timestamp = pd.Timestamp('2021-1-1 01:55:30')

3. Tạo BusinessHour Offset

bhOffset = pd.tseries.offsets.BusinessHour(start="09:30", end="18:00")

4. Hiển thị timestamp cập nhật

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

5. Lấy mã quy tắc

print("\nMã quy tắc của BusinessHour object..\n", bhOffset.rule_code)

Ví dụ hoàn chỉnh

import pandas as pd

timestamp = pd.Timestamp('2021-1-1 01:55:30')
print("Timestamp...\n", timestamp)

bhOffset = pd.tseries.offsets.BusinessHour(start="09:30", end="18:00")
print("\nBusinessHour Offset...\n", bhOffset)
print("\nUpdated Timestamp...\n", timestamp + bhOffset)
print("\nMã quy tắc của BusinessHour object..\n", bhOffset.rule_code)

Kết quả đầu ra

Timestamp...
2021-01-01 01:55:30
BusinessHour Offset...
<BusinessHour: BH=09:30-18:00>
Updated Timestamp...
2021-01-01 10:30:00
Mã quy tắc của BusinessHour object..
BH

Giải thích: Mã quy tắc BH đại diện cho BusinessHour. Các mã quy tắc phổ biến khác: B (BusinessDay), D (Day), W (Week), M (MonthEnd), H (Hour), T (Minute), S (Second).