pandas.tseries.offsets.BusinessHour#

class pandas.tseries.offsets.BusinessHour#

DateOffset subclass representing possibly n business hours.

BusinessHour is a date offset that advances time by a number of business hours. By default, business hours are from 9:00 AM to 5:00 PM on weekdays. The start and end parameters can be used to customize the business hours window, and multiple intervals can be specified by passing lists.

Parameters:
nint, default 1

The number of hours represented.

normalizebool, default False

Normalize start/end dates to midnight before generating date range.

startstr, time, or list of str/time, default “09:00”

Start time of your custom business hour in 24h format.

endstr, time, or list of str/time, default: “17:00”

End time of your custom business hour in 24h format.

offsettimedelta, default timedelta(0)

Time offset to apply.

Attributes

offset

Return the time offset applied to the business day.

holidays

Return the holidays used for custom business day calculations.

calendar

Return the calendar used for business day calculations.

weekmask

Return the weekmask used for custom business day calculations.

start

Return the start time(s) of the business hour.

end

Return the end time(s) of the business hour.

See also

CustomBusinessHour

DateOffset subclass with custom weekmask and holidays.

BusinessDay

DateOffset subclass representing possibly n business days.

Examples

You can use the parameter n to represent a shift of n hours.

>>> ts = pd.Timestamp(2022, 12, 9, 8)
>>> ts + pd.offsets.BusinessHour(n=5)
Timestamp('2022-12-09 14:00:00')

You can also change the start and the end of business hours.

>>> ts = pd.Timestamp(2022, 8, 5, 16)
>>> ts + pd.offsets.BusinessHour(start="11:00")
Timestamp('2022-08-08 11:00:00')
>>> from datetime import time as dt_time
>>> ts = pd.Timestamp(2022, 8, 5, 22)
>>> ts + pd.offsets.BusinessHour(end=dt_time(19, 0))
Timestamp('2022-08-08 10:00:00')

Passing the parameter normalize equal to True, you shift the start of the next business hour to midnight.

>>> ts = pd.Timestamp(2022, 12, 9, 8)
>>> ts + pd.offsets.BusinessHour(normalize=True)
Timestamp('2022-12-09 00:00:00')

You can divide your business day hours into several parts.

>>> import datetime as dt
>>> freq = pd.offsets.BusinessHour(start=["06:00", "10:00", "15:00"],
...                                end=["08:00", "12:00", "17:00"])
>>> pd.date_range(dt.datetime(2022, 12, 9), dt.datetime(2022, 12, 13), freq=freq)
DatetimeIndex(['2022-12-09 06:00:00', '2022-12-09 07:00:00',
               '2022-12-09 10:00:00', '2022-12-09 11:00:00',
               '2022-12-09 15:00:00', '2022-12-09 16:00:00',
               '2022-12-12 06:00:00', '2022-12-12 07:00:00',
               '2022-12-12 10:00:00', '2022-12-12 11:00:00',
               '2022-12-12 15:00:00', '2022-12-12 16:00:00'],
               dtype='datetime64[us]', freq='bh')