pandas.tseries.offsets.CustomBusinessDay#

class pandas.tseries.offsets.CustomBusinessDay#

DateOffset subclass representing possibly n custom business days.

在 CustomBusinessDay 中,我们可以使用自定义的 weekmask、holidays 和 calendar。

参数:
nint, default 1

表示的天数。

normalizebool,默认为 False

在生成日期范围之前将 start/end 日期规范化为午夜。

weekmaskstr, 默认 ‘Mon Tue Wed Thu Fri’

有效的营业日周掩码,传递给 numpy.busdaycalendar

holidayslist

要从有效营业日集中排除的日期列表/数组,传递给 numpy.busdaycalendar

calendarnp.busdaycalendar

要集成的日历。

offsettimedelta, default timedelta(0)

要应用的日期偏移量。

示例

在下面的示例中,默认参数给出下一个工作日。

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

可以通过 weekmask 参数指定工作日。在下一个示例中,使用 strftime() 函数将返回的 datetime 对象转换为其字符串表示形式。

>>> 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')

使用 NumPy 营业日日历,您可以定义自定义节假日。

>>> 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')

如果你想将结果偏移 n 天,可以使用 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')

属性

base

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

calendar

返回用于营业日计算的日历。

freqstr

Return a string representing the frequency.

holidays

返回用于自定义营业日计算的节假日。

kwds

Return a dict of extra parameters for the offset.

n

Return the count of the number of periods.

name

Return a string representing the base frequency.

nanos

Returns an integer of the total number of nanoseconds for fixed frequencies.

normalize

Return boolean whether the frequency can align with midnight.

偏移量

self._offset 的别名。

rule_code

Return a string representing the base frequency.

weekmask

返回用于自定义营业日计算的周掩码。

Methods

copy()

Return a copy of the frequency.

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.