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

Python Pandas: Trả về số lần tăng (increments) trên BusinessHour offset

Thuộc tính BusinessHour.n trong Pandas trả về số lần tăng (count of increments) đượ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", n=8)

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

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

5. Lấy số lần tăng

print("\nSố lần tăng trên BusinessHour object..\n", bhOffset.n)

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", n=8)
print("\nBusinessHour Offset...\n", bhOffset)
print("\nUpdated Timestamp...\n", timestamp + bhOffset)
print("\nSố lần tăng trên BusinessHour object..\n", bhOffset.n)

Kết quả đầu ra

Timestamp...
2021-01-01 01:55:30
BusinessHour Offset...
<8 * BusinessHours: BH=09:30-18:00>
Updated Timestamp...
2021-01-01 17:30:00
Số lần tăng trên BusinessHour object..
8

Lưu ý: Giá trị n=8 được truyền khi tạo offset, và thuộc tính .n trả về chính xác giá trị này. Điều này hữu ích khi bạn cần kiểm tra cấu hình offset trong các hàm xử lý động.