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


To display the start time of the custom business hour in 24h format from the BusinessHour offset object, use the BusinessHour.start 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 −

Display the start time of the custom business hour −

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

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 start time of the custom business hour
print("\nThe start time of the custom business hour...\n",bhOffset.start)

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 start time of the custom business hour...
 (datetime.time(9, 30),)

Updated on: 21-Oct-2021

71 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements