Computer >> Máy Tính >  >> Lập trình >> Python

Python Pandas - Hiển thị thời gian kết thúc của giờ làm việc tùy chỉnh ở định dạng 24h từ đối tượng bù đắp CustomBusinessHour

Để hiển thị thời gian kết thúc của giờ làm việc tùy chỉnh ở định dạng 24h từ đối tượng bù đắp CustomBusinessHour, hãy sử dụng thuộc tính CustomBusinessHour.end trong Pandas.

Đầu tiên, hãy nhập các thư viện được yêu cầu -

import pandas as pd

Đặt đối tượng dấu thời gian trong Pandas -

timestamp = pd.Timestamp('2021-11-14 05:20:30')

Tạo phần bù CustomBusinessHour. Ở đây, "bắt đầu" là thời gian bắt đầu của giờ làm việc tùy chỉnh của bạn trong định dạng in24h. "Kết thúc" là thời gian kết thúc của giờ làm việc tùy chỉnh của bạn ở định dạng 24h -

cbhOffset = pd.tseries.offsets.CustomBusinessHour(start="09:30", end = "18:30", n = 5)

Thêm phần bù vào Dấu thời gian và hiển thị Dấu thời gian đã cập nhật -

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

Hiển thị thời gian kết thúc của giờ làm việc tùy chỉnh -

print("\nThe end time of the custom business hour...\n",cbhOffset.end)

Ví dụ

Sau đây là mã -

import pandas as pd

# Set the timestamp object in Pandas
timestamp = pd.Timestamp('2021-11-14 05:20:30')

# Display the Timestamp
print("Timestamp...\n",timestamp)

# Create the CustomBusinessHour Offset
# CustomBusinessHour is the DateOffset subclass
# Here, "start" is the start time of your custom business hour in 24h format.
# The "end" is the end time of your custom business hour in 24h format.
cbhOffset = pd.tseries.offsets.CustomBusinessHour(start="09:30", end = "18:30", n = 5)

# Display the CustomBusinessHour Offset
print("\nCustomBusinessHour Offset...\n",cbhOffset)

# Add the offset to the Timestamp and display the Updated Timestamp
print("\nUpdated Timestamp...\n",timestamp + cbhOffset)

# Display the end time of the custom business hour
print("\nThe end time of the custom business hour...\n",cbhOffset.end)

Đầu ra

Điều này sẽ tạo ra mã sau -

Timestamp...
 2021-11-14 05:20:30

CustomBusinessHour Offset...
 <5 * CustomBusinessHours: CBH=09:30-18:30>

Updated Timestamp...
 2021-11-15 14:30:00

The end time of the custom business hour...
 (datetime.time(18, 30),)