python - 如何在不包括节假日的日期中添加工作日
问题描述
我有一个带有 start_date 列和 add_days 列(=10)的数据框(df)。我想创建(=start_date + add_days)
不包括周末和假期(假期作为数据框)的 target_date。
我做了一些研究,我试试这个。
from datetime import date, timedelta
import datetime as dt
df["star_date"] = pd.to_datetime(df["star_date"])
Holidays['Date_holi'] = pd.to_datetime(Holidays['Date_holi'])
def date_by_adding_business_days(from_date, add_days, holidays):
business_days_to_add = add_days
current_date = from_date
while business_days_to_add > 0:
current_date += datetime.timedelta(days=1)
weekday = current_date.weekday()
if weekday >= 5: # sunday = 6
continue
if current_date in holidays:
continue
business_days_to_add -= 1
return current_date
#demo:
base["Target_date"]=date_by_adding_business_days(df["start_date"], 10, Holidays['Date_holi'])
但我收到此错误:
AttributeError:“系列”对象没有属性“工作日”
谢谢你的帮助。
解决方案
ALollz 的评论非常有效;在创建期间自定义您的日期以仅保留为您的问题定义为工作日的日期将是最佳选择。
但是,我假设您无法事先定义工作日,并且您需要使用按原样构建的数据框来解决问题。
这是一种可能的解决方案:
import pandas as pd
import numpy as np
from datetime import timedelta
# Goal is to offset a start date by N business days (weekday + not a holiday)
# Here we fake the dataset as it was not provided
num_row = 1000
df = pd.DataFrame()
df['start_date'] = pd.date_range(start='1/1/1979', periods=num_row, freq='D')
df['add_days'] = pd.Series([10]*num_row)
# Define what is a week day
week_day = [0,1,2,3,4] # Monday to Friday
# Define what is a holiday with month and day without year (you can add more)
holidays = ['10-30','12-24']
def add_days_to_business_day(df, week_day, holidays, increment=10):
'''
modify the dataframe to increment only the days that are part of a weekday
and not part of a pre-defined holiday
>>> add_days_to_business_day(df, [0,1,2,3,4], ['10-31','12-31'])
this will increment by 10 the days from Monday to Friday excluding Halloween and new year-eve
'''
# Increment everything that is in a business day
df.loc[df['start_date'].dt.dayofweek.isin(week_day),'target_date'] = df['start_date'] + timedelta(days=increment)
# Remove every increment done on a holiday
df.loc[df['start_date'].dt.strftime('%m-%d').isin(holidays), 'target_date'] = np.datetime64('NaT')
add_days_to_business_day(df, week_day, holidays)
df
注意:我没有使用“add_days”列,因为它只是一个重复值。我改为使用我的函数增量参数,该参数将增加N天(默认为 N = 10)。
希望能帮助到你!
推荐阅读
- ruby - ruby 语句中的组件是什么
- docusaurus - 访问 sidebars.json (docusaurus) 中未列出的文档时显示侧边栏
- java - 在Android中单击地图OpenStreetMap设置标记
- bash - Bash sum while 循环并不总是给出相同的输出
- android - 二进制 XML 文件第 35 行:膨胀类 TextView 时出错
- php - Prestashop 1.7 适应来自多个模块的覆盖
- c# - 如何在asp.net的gridview中添加嵌套的json数组?
- linux - 使用 8 位颜色在 Linux 上正确显示 Qt Gui。对话框中的点
- r - x.scores 和 y.scores 在 R 的偏最小二乘回归中代表什么?
- vb.net - VB.NET 中的非结构化异常处理