📅  最后修改于: 2023-12-03 15:34:16.672000             🧑  作者: Mango
CustomBusinessDay
是Pandas中的时间序列偏移对象,可根据自定义的工作日或假期日历进行调整。
CustomBusinessDay
。from pandas.tseries.offsets import CustomBusinessDay
holidays = ['2021-09-20', '2021-10-01', '2021-10-04']
custom_bd = CustomBusinessDay(holidays=holidays)
from pandas import Timestamp
start_date = Timestamp('2021-09-18')
end_date = start_date + 2*custom_bd
print(end_date)
输出结果为:2021-09-23 00:00:00
n
: 指定偏移量,默认为1.weekmask
: 可以指定每周的哪些工作日。默认为周一至周五为工作日,使用二进制方式(00000表示周六周日为非工作日,11111表示每日为工作日)。holidays
: 指定假期的日期列表。# 导入相关函数库
import pandas as pd
from pandas.tseries.offsets import CustomBusinessDay
# 设置自定义工作日对象,周末和2022年2月1号设为假期
my_holidays = ['2022-01-01', '2022-01-02', '2022-01-03', '2022-01-31']
my_weekmask = 'Mon Tue Wed Thu Fri'
my_offset = CustomBusinessDay(holidays=my_holidays, weekmask=my_weekmask)
# 创建数据框
data = {'Price': [10, 11, 12, 11, 12, 9, 10, 14],
'Date': ['2022-01-17', '2022-01-18', '2022-01-19', '2022-01-20',
'2022-01-21', '2022-01-24', '2022-01-25', '2022-01-26']}
df = pd.DataFrame(data)
df['Date'] = pd.to_datetime(df['Date'])
# 使用偏移量调整时间序列
df['Next Business Day'] = df['Date'] + my_offset
print(df)
输出结果:
Price Date Next Business Day
0 10 2022-01-17 2022-01-18
1 11 2022-01-18 2022-01-19
2 12 2022-01-19 2022-01-20
3 11 2022-01-20 2022-01-24
4 12 2022-01-21 2022-01-24
5 9 2022-01-24 2022-01-25
6 10 2022-01-25 2022-01-26
7 14 2022-01-26 2022-01-27
以上是 Python | Pandas tseries.offsets.CustomBusinessDay
的基本使用方法,希望本文可以为大家提供帮助。