From 63e51f0a0509d97214b361929eb81b14742ba218 Mon Sep 17 00:00:00 2001 From: Natalia Mokeeva Date: Sat, 10 Dec 2022 23:49:15 +0100 Subject: [PATCH 1/4] DOC add examples CustomBusinessHour I --- pandas/_libs/tslibs/offsets.pyx | 83 +++++++++++++++++++++++++++++++-- 1 file changed, 78 insertions(+), 5 deletions(-) diff --git a/pandas/_libs/tslibs/offsets.pyx b/pandas/_libs/tslibs/offsets.pyx index 482cf91c92b70..f87f813af63ca 100644 --- a/pandas/_libs/tslibs/offsets.pyx +++ b/pandas/_libs/tslibs/offsets.pyx @@ -3619,6 +3619,8 @@ cdef class CustomBusinessHour(BusinessHour): """ DateOffset subclass representing possibly n custom business days. + In CustomBusinessHour we can use custom weekmask, holidays, and calendar. + Parameters ---------- n : int, default 1 @@ -3627,24 +3629,95 @@ cdef class CustomBusinessHour(BusinessHour): Normalize start/end dates to midnight before generating date range. weekmask : str, Default 'Mon Tue Wed Thu Fri' Weekmask of valid business days, passed to ``numpy.busdaycalendar``. + holidays : list + List/array of dates to exclude from the set of valid business days, + passed to ``numpy.busdaycalendar``. + calendar : np.busdaycalendar + Calendar to integrate. start : str, time, or list of str/time, default "09:00" Start time of your custom business hour in 24h format. end : str, time, or list of str/time, default: "17:00" End time of your custom business hour in 24h format. + offset : timedelta, default timedelta(0) + Time offset to apply. Examples -------- - >>> from datetime import time + In the example below the default parameters give as 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, 15) + >>> ts + pd.offsets.CustomBusinessHour(start="11:00") + Timestamp('2022-08-05 16:00:00') + + >>> ts = pd.Timestamp(2022, 8, 5, 16) >>> ts + pd.offsets.CustomBusinessHour(start="11:00") Timestamp('2022-08-08 11:00:00') - >>> ts + pd.offsets.CustomBusinessHour(end=time(19, 0)) + + >>> 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.offsets.CustomBusinessHour(start=[time(9, 0), "20:00"], - ... end=["17:00", time(22, 0)]) - Timestamp('2022-08-05 20:00:00') + + >>> from datetime import time as dt_time + >>> ts = pd.Timestamp(2022, 8, 5, 22) + >>> ts + pd.offsets.CustomBusinessHour(end=dt_time(19, 0)) + Timestamp('2022-08-08 10:00:00') + + In the example below the business hours start at the default value "09:00" + and end at "8:00". + + >>> ts = pd.Timestamp(2022, 8, 5, 22) + >>> ts + pd.offsets.CustomBusinessHour(end="8:00") + Timestamp('2022-08-05 23:00:00') + + >>> pd.offsets.CustomBusinessHour(end="8:00") + + + In the example below we devide our 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[ns]', freq='CBH') + + Business days can be specified by ``weekmask`` parameter. + + >>> 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) + DatetimeIndex(['2022-12-12 10:00:00', '2022-12-12 11:00:00', + '2022-12-12 12:00:00', '2022-12-14 10:00:00', + '2022-12-14 11:00:00', '2022-12-14 12:00:00', + '2022-12-16 10:00:00', '2022-12-16 11:00:00', + '2022-12-16 12:00:00'], + dtype='datetime64[ns]', freq='CBH') + + In the example below we define custom holidays by using NumPy business day calendar. + + >>> 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[ns]', freq='CBH') """ _prefix = "CBH" From 2d72295bcdf2a962e9f5f8918e6ba1d0fc0b351b Mon Sep 17 00:00:00 2001 From: Natalia Mokeeva <91160475+natmokval@users.noreply.github.com> Date: Sun, 11 Dec 2022 20:48:53 +0100 Subject: [PATCH 2/4] Update pandas/_libs/tslibs/offsets.pyx Co-authored-by: Marco Edward Gorelli <33491632+MarcoGorelli@users.noreply.github.com> --- pandas/_libs/tslibs/offsets.pyx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/_libs/tslibs/offsets.pyx b/pandas/_libs/tslibs/offsets.pyx index f87f813af63ca..b262772b342dd 100644 --- a/pandas/_libs/tslibs/offsets.pyx +++ b/pandas/_libs/tslibs/offsets.pyx @@ -3643,7 +3643,7 @@ cdef class CustomBusinessHour(BusinessHour): Examples -------- - In the example below the default parameters give as the next business hour. + In the example below the default parameters give the next business hour. >>> ts = pd.Timestamp(2022, 8, 5, 16) >>> ts + pd.offsets.CustomBusinessHour() From 438004938a11572d3af37964a3c9f9815d9a2cc6 Mon Sep 17 00:00:00 2001 From: Natalia Mokeeva Date: Mon, 12 Dec 2022 15:39:14 +0100 Subject: [PATCH 3/4] DOC add examples CustomBusinessHour II --- pandas/_libs/tslibs/offsets.pyx | 28 ++++++++++++---------------- 1 file changed, 12 insertions(+), 16 deletions(-) diff --git a/pandas/_libs/tslibs/offsets.pyx b/pandas/_libs/tslibs/offsets.pyx index b262772b342dd..2b9f2e046c9c0 100644 --- a/pandas/_libs/tslibs/offsets.pyx +++ b/pandas/_libs/tslibs/offsets.pyx @@ -3638,8 +3638,6 @@ cdef class CustomBusinessHour(BusinessHour): Start time of your custom business hour in 24h format. end : str, time, or list of str/time, default: "17:00" End time of your custom business hour in 24h format. - offset : timedelta, default timedelta(0) - Time offset to apply. Examples -------- @@ -3651,10 +3649,6 @@ cdef class CustomBusinessHour(BusinessHour): We can also change the start and the end of business hours. - >>> ts = pd.Timestamp(2022, 8, 5, 15) - >>> ts + pd.offsets.CustomBusinessHour(start="11:00") - Timestamp('2022-08-05 16:00:00') - >>> ts = pd.Timestamp(2022, 8, 5, 16) >>> ts + pd.offsets.CustomBusinessHour(start="11:00") Timestamp('2022-08-08 11:00:00') @@ -3664,7 +3658,6 @@ cdef class CustomBusinessHour(BusinessHour): >>> ts + pd.offsets.CustomBusinessHour(end=dt_time(19, 0)) Timestamp('2022-08-05 17:00:00') - >>> from datetime import time as dt_time >>> ts = pd.Timestamp(2022, 8, 5, 22) >>> ts + pd.offsets.CustomBusinessHour(end=dt_time(19, 0)) Timestamp('2022-08-08 10:00:00') @@ -3679,7 +3672,7 @@ cdef class CustomBusinessHour(BusinessHour): >>> pd.offsets.CustomBusinessHour(end="8:00") - In the example below we devide our business day hours into several parts. + In the example below we divide our business day hours into several parts. >>> import datetime as dt >>> freq = pd.offsets.CustomBusinessHour(start=["06:00", "10:00", "15:00"], @@ -3693,18 +3686,21 @@ cdef class CustomBusinessHour(BusinessHour): '2022-12-12 15:00:00', '2022-12-12 16:00:00'], dtype='datetime64[ns]', freq='CBH') - Business days can be specified by ``weekmask`` parameter. + Business days can be specified by ``weekmask`` parameter. 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) - DatetimeIndex(['2022-12-12 10:00:00', '2022-12-12 11:00:00', - '2022-12-12 12:00:00', '2022-12-14 10:00:00', - '2022-12-14 11:00:00', '2022-12-14 12:00:00', - '2022-12-16 10:00:00', '2022-12-16 11:00:00', - '2022-12-16 12:00:00'], - dtype='datetime64[ns]', freq='CBH') + >>> 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='object') In the example below we define custom holidays by using NumPy business day calendar. From 1bf675d81667f5d69d5c3df691c1a7e6a7f3fc7e Mon Sep 17 00:00:00 2001 From: Natalia Mokeeva Date: Mon, 12 Dec 2022 19:32:14 +0100 Subject: [PATCH 4/4] DOC add examples CustomBusinessHour III --- pandas/_libs/tslibs/offsets.pyx | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/pandas/_libs/tslibs/offsets.pyx b/pandas/_libs/tslibs/offsets.pyx index 2b9f2e046c9c0..cd4fc7405a41f 100644 --- a/pandas/_libs/tslibs/offsets.pyx +++ b/pandas/_libs/tslibs/offsets.pyx @@ -3662,16 +3662,6 @@ cdef class CustomBusinessHour(BusinessHour): >>> ts + pd.offsets.CustomBusinessHour(end=dt_time(19, 0)) Timestamp('2022-08-08 10:00:00') - In the example below the business hours start at the default value "09:00" - and end at "8:00". - - >>> ts = pd.Timestamp(2022, 8, 5, 22) - >>> ts + pd.offsets.CustomBusinessHour(end="8:00") - Timestamp('2022-08-05 23:00:00') - - >>> pd.offsets.CustomBusinessHour(end="8:00") - - In the example below we divide our business day hours into several parts. >>> import datetime as dt