Python Pandas - Create a CustomBusinessHour Offset object

To create a CustomBusinessHour object, use the pandas.tseries.offsets.CustomBusinessHour() method in Pandas. This offset allows you to define custom business hours with specific weekdays and time ranges.

Syntax

pandas.tseries.offsets.CustomBusinessHour(n=1, normalize=False, start='09:00', end='17:00', weekmask='Mon Tue Wed Thu Fri', calendar=None, offset=timedelta(0))

Parameters

  • n ? Number of business hours to offset (default: 1)
  • weekmask ? Valid business days as a string (default: 'Mon Tue Wed Thu Fri')
  • start ? Start time of business hours (default: '09:00')
  • end ? End time of business hours (default: '17:00')

Basic Example

Let's create a CustomBusinessHour offset with custom weekdays ?

import pandas as pd

# Set the timestamp object in Pandas
timestamp = pd.Timestamp('2021-12-31 08:35:10')

# Display the Timestamp
print("Timestamp...")
print(timestamp)

# Create the CustomBusinessHour Offset
# CustomBusinessHour is the DateOffset subclass
# Weekmask of valid business days
cbhOffset = pd.tseries.offsets.CustomBusinessHour(n=5, weekmask='Mon Tue Wed Fri')

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

# Add the offset to the Timestamp and display the Updated Timestamp
print("\nUpdated Timestamp...")
print(timestamp + cbhOffset)
Timestamp...
2021-12-31 08:35:10

CustomBusinessHour Offset...
<5 * CustomBusinessHours: CBH=09:00-17:00>

Updated Timestamp...
2021-12-31 14:00:00

Custom Business Hours with Different Time Range

You can also specify custom start and end times for business hours ?

import pandas as pd

# Create timestamp
timestamp = pd.Timestamp('2023-01-16 10:00:00')  # Monday

# Create CustomBusinessHour with custom time range
cbh_custom = pd.tseries.offsets.CustomBusinessHour(
    n=3, 
    start='08:00', 
    end='16:00',
    weekmask='Mon Tue Wed Thu Fri'
)

print("Original Timestamp:")
print(timestamp)
print("\nCustomBusinessHour with 8AM-4PM:")
print(cbh_custom)
print("\nResult after adding 3 hours:")
print(timestamp + cbh_custom)
Original Timestamp:
2023-01-16 10:00:00

CustomBusinessHour with 8AM-4PM:
<3 * CustomBusinessHours: CBH=08:00-16:00>

Result after adding 3 hours:
2023-01-16 13:00:00

Key Points

  • CustomBusinessHour automatically skips non-business days defined in the weekmask
  • Time additions that exceed business hours roll over to the next business day
  • The default business hours are 9:00 AM to 5:00 PM
  • You can customize both the weekdays and the time range

Conclusion

CustomBusinessHour offset in Pandas allows you to perform time arithmetic while respecting custom business days and hours. Use the weekmask parameter to define valid business days and start/end parameters to set custom business hours.

Updated on: 2026-03-26T18:10:37+05:30

205 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements