pandas.tseries.offsets.CustomBusinessDay#

classpandas.tseries.offsets.CustomBusinessDay#

DateOffset subclass representing possibly n custom business days.

In CustomBusinessDay we can use custom weekmask, holidays, and calendar.

Parameters:
nint, default 1

The number of days 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.

offsettimedelta, default timedelta(0)

Time offset to apply.

Examples

In the example below the default parameters give the next business day.

>>> ts = pd.Timestamp(2022, 8, 5, 16)
>>> ts + pd.offsets.CustomBusinessDay()
Timestamp('2022年08月08日 16:00:00')

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.CustomBusinessDay(weekmask="Mon Wed Fri")
>>> pd.date_range(dt.datetime(2022, 12, 10), dt.datetime(2022, 12, 21),
...  freq=freq).strftime('%a%d %b %Y %H:%M')
Index(['Mon 12 Dec 2022 00:00', 'Wed 14 Dec 2022 00:00',
 'Fri 16 Dec 2022 00:00', 'Mon 19 Dec 2022 00:00',
 'Wed 21 Dec 2022 00:00'],
 dtype='object')

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.CustomBusinessDay(calendar=bdc)
>>> pd.date_range(dt.datetime(2022, 12, 10), dt.datetime(2022, 12, 25), freq=freq)
DatetimeIndex(['2022年12月13日', '2022年12月15日', '2022年12月16日', '2022年12月19日',
 '2022年12月20日', '2022年12月21日', '2022年12月22日', '2022年12月23日'],
 dtype='datetime64[ns]', freq='C')

If you want to shift the result on n day you can use the parameter offset.

>>> pd.Timestamp(2022, 8, 5, 16) + pd.offsets.CustomBusinessDay(1)
Timestamp('2022年08月08日 16:00:00')
>>> import datetime as dt
>>> ts = pd.Timestamp(2022, 8, 5, 16)
>>> ts + pd.offsets.CustomBusinessDay(1, offset=dt.timedelta(days=1))
Timestamp('2022年08月09日 16:00:00')

Attributes

base

Returns a copy of the calling offset object with n=1 and all other attributes equal.

freqstr

Return a string representing the frequency.

kwds

Return a dict of extra parameters for the offset.

n

name

Return a string representing the base frequency.

offset

Alias for self._offset.

Methods

copy()

Return a copy of the frequency.

is_anchored()

(DEPRECATED) Return boolean whether the frequency is a unit frequency (n=1).

is_month_end(ts)

Return boolean whether a timestamp occurs on the month end.

is_month_start(ts)

Return boolean whether a timestamp occurs on the month start.

is_on_offset(dt)

Return boolean whether a timestamp intersects with this frequency.

is_quarter_end(ts)

Return boolean whether a timestamp occurs on the quarter end.

is_quarter_start(ts)

Return boolean whether a timestamp occurs on the quarter start.

is_year_end(ts)

Return boolean whether a timestamp occurs on the year end.

is_year_start(ts)

Return boolean whether a timestamp occurs on the year start.

rollback(dt)

Roll provided date backward to next offset only if not on offset.

rollforward(dt)

Roll provided date forward to next offset only if not on offset.