pandas.tseries.offsets.CustomBusinessHour#
- class pandas.tseries.offsets.CustomBusinessHour#
DateOffset subclass representing possibly n custom business days.
In CustomBusinessHour we can use custom weekmask, holidays, and calendar.
- Parameters:
- nint, default 1
The number of hours represented.
- normalizebool, default False
Normalize start/end dates to midnight before generating date range.
- weekmaskstr, Default ‘Mon Tue Wed Thu Fri’
Weekmask of valid business days, passed to
numpy.busdaycalendar.- holidayslist
List/array of dates to exclude from the set of valid business days, passed to
numpy.busdaycalendar.- calendarnp.busdaycalendar
Calendar to integrate.
- 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
Return the weekmask used for custom business day calculations.
Return the holidays used for custom business day calculations.
Return the calendar used for business day calculations.
Return the start time(s) of the business hour.
Return the end time(s) of the business hour.
Return the time offset applied to the business day.
See also
BusinessHourDateOffset subclass representing possibly n business hours.
CustomBusinessDayDateOffset subclass representing custom business days.
Examples
In the example below the default parameters give the next business hour.
>>> ts = pd.Timestamp(2022, 8, 5, 16) >>> ts + pd.offsets.CustomBusinessHour() Timestamp('2022-08-08 09:00:00')
We can also change the start and the end of business hours.
>>> ts = pd.Timestamp(2022, 8, 5, 16) >>> ts + pd.offsets.CustomBusinessHour(start="11:00") Timestamp('2022-08-08 11:00:00')
>>> from datetime import time as dt_time >>> ts = pd.Timestamp(2022, 8, 5, 16) >>> ts + pd.offsets.CustomBusinessHour(end=dt_time(19, 0)) Timestamp('2022-08-05 17:00:00')
>>> ts = pd.Timestamp(2022, 8, 5, 22) >>> ts + pd.offsets.CustomBusinessHour(end=dt_time(19, 0)) Timestamp('2022-08-08 10:00:00')
You can divide your business day hours into several parts.
>>> import datetime as dt >>> freq = pd.offsets.CustomBusinessHour(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='cbh')
Business days can be specified by
weekmaskparameter. To convert the returned datetime object to its string representation the function strftime() is used in the next example.>>> import datetime as dt >>> freq = pd.offsets.CustomBusinessHour(weekmask="Mon Wed Fri", ... start="10:00", end="13:00") >>> pd.date_range(dt.datetime(2022, 12, 10), dt.datetime(2022, 12, 18), ... freq=freq).strftime('%a %d %b %Y %H:%M') Index(['Mon 12 Dec 2022 10:00', 'Mon 12 Dec 2022 11:00', 'Mon 12 Dec 2022 12:00', 'Wed 14 Dec 2022 10:00', 'Wed 14 Dec 2022 11:00', 'Wed 14 Dec 2022 12:00', 'Fri 16 Dec 2022 10:00', 'Fri 16 Dec 2022 11:00', 'Fri 16 Dec 2022 12:00'], dtype='str')
Using NumPy business day calendar you can define custom holidays.
>>> import datetime as dt >>> bdc = np.busdaycalendar(holidays=['2022-12-12', '2022-12-14']) >>> freq = pd.offsets.CustomBusinessHour(calendar=bdc, start="10:00", end="13:00") >>> pd.date_range(dt.datetime(2022, 12, 10), dt.datetime(2022, 12, 18), freq=freq) DatetimeIndex(['2022-12-13 10:00:00', '2022-12-13 11:00:00', '2022-12-13 12:00:00', '2022-12-15 10:00:00', '2022-12-15 11:00:00', '2022-12-15 12:00:00', '2022-12-16 10:00:00', '2022-12-16 11:00:00', '2022-12-16 12:00:00'], dtype='datetime64[us]', freq='cbh')