Skip to content

Commit d27f47b

Browse files
DOC: update documentation for CustomBusinessHour (#50182)
* DOC add examples CustomBusinessHour I * Update pandas/_libs/tslibs/offsets.pyx Co-authored-by: Marco Edward Gorelli <[email protected]> * DOC add examples CustomBusinessHour II * DOC add examples CustomBusinessHour III Co-authored-by: Marco Edward Gorelli <[email protected]>
1 parent 49f93ed commit d27f47b

File tree

1 file changed

+64
-5
lines changed

1 file changed

+64
-5
lines changed

pandas/_libs/tslibs/offsets.pyx

+64-5
Original file line numberDiff line numberDiff line change
@@ -3619,6 +3619,8 @@ cdef class CustomBusinessHour(BusinessHour):
36193619
"""
36203620
DateOffset subclass representing possibly n custom business days.
36213621
3622+
In CustomBusinessHour we can use custom weekmask, holidays, and calendar.
3623+
36223624
Parameters
36233625
----------
36243626
n : int, default 1
@@ -3627,24 +3629,81 @@ cdef class CustomBusinessHour(BusinessHour):
36273629
Normalize start/end dates to midnight before generating date range.
36283630
weekmask : str, Default 'Mon Tue Wed Thu Fri'
36293631
Weekmask of valid business days, passed to ``numpy.busdaycalendar``.
3632+
holidays : list
3633+
List/array of dates to exclude from the set of valid business days,
3634+
passed to ``numpy.busdaycalendar``.
3635+
calendar : np.busdaycalendar
3636+
Calendar to integrate.
36303637
start : str, time, or list of str/time, default "09:00"
36313638
Start time of your custom business hour in 24h format.
36323639
end : str, time, or list of str/time, default: "17:00"
36333640
End time of your custom business hour in 24h format.
36343641
36353642
Examples
36363643
--------
3637-
>>> from datetime import time
3644+
In the example below the default parameters give the next business hour.
3645+
36383646
>>> ts = pd.Timestamp(2022, 8, 5, 16)
36393647
>>> ts + pd.offsets.CustomBusinessHour()
36403648
Timestamp('2022-08-08 09:00:00')
3649+
3650+
We can also change the start and the end of business hours.
3651+
3652+
>>> ts = pd.Timestamp(2022, 8, 5, 16)
36413653
>>> ts + pd.offsets.CustomBusinessHour(start="11:00")
36423654
Timestamp('2022-08-08 11:00:00')
3643-
>>> ts + pd.offsets.CustomBusinessHour(end=time(19, 0))
3655+
3656+
>>> from datetime import time as dt_time
3657+
>>> ts = pd.Timestamp(2022, 8, 5, 16)
3658+
>>> ts + pd.offsets.CustomBusinessHour(end=dt_time(19, 0))
36443659
Timestamp('2022-08-05 17:00:00')
3645-
>>> ts + pd.offsets.CustomBusinessHour(start=[time(9, 0), "20:00"],
3646-
... end=["17:00", time(22, 0)])
3647-
Timestamp('2022-08-05 20:00:00')
3660+
3661+
>>> ts = pd.Timestamp(2022, 8, 5, 22)
3662+
>>> ts + pd.offsets.CustomBusinessHour(end=dt_time(19, 0))
3663+
Timestamp('2022-08-08 10:00:00')
3664+
3665+
In the example below we divide our business day hours into several parts.
3666+
3667+
>>> import datetime as dt
3668+
>>> freq = pd.offsets.CustomBusinessHour(start=["06:00", "10:00", "15:00"],
3669+
... end=["08:00", "12:00", "17:00"])
3670+
>>> pd.date_range(dt.datetime(2022, 12, 9), dt.datetime(2022, 12, 13), freq=freq)
3671+
DatetimeIndex(['2022-12-09 06:00:00', '2022-12-09 07:00:00',
3672+
'2022-12-09 10:00:00', '2022-12-09 11:00:00',
3673+
'2022-12-09 15:00:00', '2022-12-09 16:00:00',
3674+
'2022-12-12 06:00:00', '2022-12-12 07:00:00',
3675+
'2022-12-12 10:00:00', '2022-12-12 11:00:00',
3676+
'2022-12-12 15:00:00', '2022-12-12 16:00:00'],
3677+
dtype='datetime64[ns]', freq='CBH')
3678+
3679+
Business days can be specified by ``weekmask`` parameter. To convert
3680+
the returned datetime object to its string representation
3681+
the function strftime() is used in the next example.
3682+
3683+
>>> import datetime as dt
3684+
>>> freq = pd.offsets.CustomBusinessHour(weekmask="Mon Wed Fri",
3685+
... start="10:00", end="13:00")
3686+
>>> pd.date_range(dt.datetime(2022, 12, 10), dt.datetime(2022, 12, 18),
3687+
... freq=freq).strftime('%a %d %b %Y %H:%M')
3688+
Index(['Mon 12 Dec 2022 10:00', 'Mon 12 Dec 2022 11:00',
3689+
'Mon 12 Dec 2022 12:00', 'Wed 14 Dec 2022 10:00',
3690+
'Wed 14 Dec 2022 11:00', 'Wed 14 Dec 2022 12:00',
3691+
'Fri 16 Dec 2022 10:00', 'Fri 16 Dec 2022 11:00',
3692+
'Fri 16 Dec 2022 12:00'],
3693+
dtype='object')
3694+
3695+
In the example below we define custom holidays by using NumPy business day calendar.
3696+
3697+
>>> import datetime as dt
3698+
>>> bdc = np.busdaycalendar(holidays=['2022-12-12', '2022-12-14'])
3699+
>>> freq = pd.offsets.CustomBusinessHour(calendar=bdc, start="10:00", end="13:00")
3700+
>>> pd.date_range(dt.datetime(2022, 12, 10), dt.datetime(2022, 12, 18), freq=freq)
3701+
DatetimeIndex(['2022-12-13 10:00:00', '2022-12-13 11:00:00',
3702+
'2022-12-13 12:00:00', '2022-12-15 10:00:00',
3703+
'2022-12-15 11:00:00', '2022-12-15 12:00:00',
3704+
'2022-12-16 10:00:00', '2022-12-16 11:00:00',
3705+
'2022-12-16 12:00:00'],
3706+
dtype='datetime64[ns]', freq='CBH')
36483707
"""
36493708

36503709
_prefix = "CBH"

0 commit comments

Comments
 (0)