Để hiển thị thời gian bắt đầu 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.start 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 ở định dạng 24h. "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:00", n = 8)
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 bắt đầu của giờ làm việc tùy chỉnh -
print("\nThe start time of the custom business hour...\n",cbhOffset.start) 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:00", n = 8)
# 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 start time of the custom business hour
print("\nThe start time of the custom business hour...\n",cbhOffset.start) Đầu ra
Điều này sẽ tạo ra mã sau -
Timestamp... 2021-11-14 05:20:30 CustomBusinessHour Offset... <8 * CustomBusinessHours: CBH=09:30-18:00> Updated Timestamp... 2021-11-15 17:30:00 The start time of the custom business hour... (datetime.time(9, 30),)