Python Pandas - Display the end time of the custom business hour in 24h format from the BusinessHour offset object


To display the end time of the custom business hour in 24h format from the BusinessHour offset object, use the BusinessHour.end property.

At first, import the required libraries −

import pandas as pd

Set the timestamp object in Pandas −

timestamp = pd.Timestamp('2021-9-30 06:50:20')

Create the BusinessHour Offset. 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 −

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

Display the Updated Timestamp −

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

Display the end time of the custom business hour −

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

Example

Following is the code −

import pandas as pd

# Set the timestamp object in Pandas
timestamp = pd.Timestamp('2021-9-30 06:50:20')

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

# Create the BusinessHour Offset
# BusinessHour 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.
bhOffset = pd.tseries.offsets.BusinessHour(start="09:30", end = "18:00", n = 8)

# Display the BusinessHour Offset
print("\nBusinessHour Offset...\n",bhOffset)

# Display the Updated Timestamp
print("\nUpdated Timestamp...\n",timestamp + bhOffset)

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

Output

This will produce the following code −

Timestamp...
 2021-09-30 06:50:20

BusinessHour Offset...
 <8 * BusinessHours: BH=09:30-18:00>

Updated Timestamp...
 2021-09-30 17:30:00

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

Updated on: 21-Oct-2021

56 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements