Skip to content

Commit e9a2eb8

Browse files
authored
DOC: add examples to offsets CustomBusinessDay, Business/MonthBegin (#51932)
* DOC: add examples to offsets CustomBusinessDay, BusinessMonthBegin, MonthBegin * DOC: add the parameter offset to CustomBusinessDay
1 parent d1c43dc commit e9a2eb8

File tree

1 file changed

+80
-14
lines changed

1 file changed

+80
-14
lines changed

pandas/_libs/tslibs/offsets.pyx

+80-14
Original file line numberDiff line numberDiff line change
@@ -2574,11 +2574,28 @@ cdef class MonthBegin(MonthOffset):
25742574
"""
25752575
DateOffset of one month at beginning.
25762576
2577+
MonthBegin goes to the next date which is a start of the month.
2578+
To get the start of the current month pass the parameter n equals 0.
2579+
2580+
See Also
2581+
--------
2582+
:class:`~pandas.tseries.offsets.DateOffset` : Standard kind of date increment.
2583+
25772584
Examples
25782585
--------
2579-
>>> ts = pd.Timestamp(2022, 1, 1)
2586+
>>> ts = pd.Timestamp(2022, 11, 30)
2587+
>>> ts + pd.offsets.MonthBegin()
2588+
Timestamp('2022-12-01 00:00:00')
2589+
2590+
>>> ts = pd.Timestamp(2022, 12, 1)
25802591
>>> ts + pd.offsets.MonthBegin()
2581-
Timestamp('2022-02-01 00:00:00')
2592+
Timestamp('2023-01-01 00:00:00')
2593+
2594+
If you want to get the start of the current month pass the parameter n equals 0:
2595+
2596+
>>> ts = pd.Timestamp(2022, 12, 1)
2597+
>>> ts + pd.offsets.MonthBegin(0)
2598+
Timestamp('2022-12-01 00:00:00')
25822599
"""
25832600
_prefix = "MS"
25842601
_day_opt = "start"
@@ -2616,16 +2633,26 @@ cdef class BusinessMonthBegin(MonthOffset):
26162633
"""
26172634
DateOffset of one month at the first business day.
26182635
2636+
BusinessMonthBegin goes to the next date which is the first business day
2637+
of the month. To get the first business day of the current month pass
2638+
the parameter n equals 0.
2639+
26192640
Examples
26202641
--------
2621-
>>> from pandas.tseries.offsets import BMonthBegin
2622-
>>> ts=pd.Timestamp('2020-05-24 05:01:15')
2623-
>>> ts + BMonthBegin()
2624-
Timestamp('2020-06-01 05:01:15')
2625-
>>> ts + BMonthBegin(2)
2626-
Timestamp('2020-07-01 05:01:15')
2627-
>>> ts + BMonthBegin(-3)
2628-
Timestamp('2020-03-02 05:01:15')
2642+
>>> ts = pd.Timestamp(2022, 11, 30)
2643+
>>> ts + pd.offsets.BMonthBegin()
2644+
Timestamp('2022-12-01 00:00:00')
2645+
2646+
>>> ts = pd.Timestamp(2022, 12, 1)
2647+
>>> ts + pd.offsets.BMonthBegin()
2648+
Timestamp('2023-01-02 00:00:00')
2649+
2650+
If you want to get the start of the current business month pass
2651+
the parameter n equals 0:
2652+
2653+
>>> ts = pd.Timestamp(2022, 12, 1)
2654+
>>> ts + pd.offsets.BMonthBegin(0)
2655+
Timestamp('2022-12-01 00:00:00')
26292656
"""
26302657
_prefix = "BMS"
26312658
_day_opt = "business_start"
@@ -3671,7 +3698,9 @@ cdef class Easter(SingleConstructorOffset):
36713698

36723699
cdef class CustomBusinessDay(BusinessDay):
36733700
"""
3674-
DateOffset subclass representing custom business days excluding holidays.
3701+
DateOffset subclass representing possibly n custom business days.
3702+
3703+
In CustomBusinessDay we can use custom weekmask, holidays, and calendar.
36753704
36763705
Parameters
36773706
----------
@@ -3685,13 +3714,50 @@ cdef class CustomBusinessDay(BusinessDay):
36853714
List/array of dates to exclude from the set of valid business days,
36863715
passed to ``numpy.busdaycalendar``.
36873716
calendar : np.busdaycalendar
3717+
Calendar to integrate.
36883718
offset : timedelta, default timedelta(0)
3719+
Time offset to apply.
36893720
36903721
Examples
36913722
--------
3692-
>>> ts = pd.Timestamp(2022, 8, 5)
3693-
>>> ts + pd.offsets.CustomBusinessDay(1)
3694-
Timestamp('2022-08-08 00:00:00')
3723+
In the example below the default parameters give the next business day.
3724+
3725+
>>> ts = pd.Timestamp(2022, 8, 5, 16)
3726+
>>> ts + pd.offsets.CustomBusinessDay()
3727+
Timestamp('2022-08-08 16:00:00')
3728+
3729+
Business days can be specified by ``weekmask`` parameter. To convert
3730+
the returned datetime object to its string representation
3731+
the function strftime() is used in the next example.
3732+
3733+
>>> import datetime as dt
3734+
>>> freq = pd.offsets.CustomBusinessDay(weekmask="Mon Wed Fri")
3735+
>>> pd.date_range(dt.datetime(2022, 12, 10), dt.datetime(2022, 12, 21),
3736+
... freq=freq).strftime('%a %d %b %Y %H:%M')
3737+
Index(['Mon 12 Dec 2022 00:00', 'Wed 14 Dec 2022 00:00',
3738+
'Fri 16 Dec 2022 00:00', 'Mon 19 Dec 2022 00:00',
3739+
'Wed 21 Dec 2022 00:00'],
3740+
dtype='object')
3741+
3742+
Using NumPy business day calendar you can define custom holidays.
3743+
3744+
>>> import datetime as dt
3745+
>>> bdc = np.busdaycalendar(holidays=['2022-12-12', '2022-12-14'])
3746+
>>> freq = pd.offsets.CustomBusinessDay(calendar=bdc)
3747+
>>> pd.date_range(dt.datetime(2022, 12, 10), dt.datetime(2022, 12, 25), freq=freq)
3748+
DatetimeIndex(['2022-12-13', '2022-12-15', '2022-12-16', '2022-12-19',
3749+
'2022-12-20', '2022-12-21', '2022-12-22', '2022-12-23'],
3750+
dtype='datetime64[ns]', freq='C')
3751+
3752+
If you want to shift the result on n day you can use the parameter ``offset``.
3753+
3754+
>>> pd.Timestamp(2022, 8, 5, 16) + pd.offsets.CustomBusinessDay(1)
3755+
Timestamp('2022-08-08 16:00:00')
3756+
3757+
>>> import datetime as dt
3758+
>>> ts = pd.Timestamp(2022, 8, 5, 16)
3759+
>>> ts + pd.offsets.CustomBusinessDay(1, offset=dt.timedelta(days=1))
3760+
Timestamp('2022-08-09 16:00:00')
36953761
"""
36963762

36973763
_prefix = "C"

0 commit comments

Comments
 (0)