1

我有一个数据集,我需要计算从给定日期到今天的工作日,不包括给定的假期列表。我将包括周末。

日期样本:

在此处输入图像描述

这是我试过的代码:

import pandas as pd
import numpy as np
from datetime import date
df = pd.read_excel('C:\\sample.xlsx')

#get todays date
df["today"] = date.today()
#Convert data type
start = df["R_REL_DATE"].values.astype('datetime64[D]')
end = df["today"].values.astype('datetime64[D]')
holiday = ['2021-06-19', '2021-06-20']
#Numpy function to find in between days
days = np.busday_count(start, end, weekmask='1111111', holidays=holiday)
#Add this column to dataframe
df["Days"] = days
df

当我运行此代码时,它会给出 R_REL_DATE 和今天之间的差异,但不会减去给定的假期。 在此处输入图像描述

请帮忙,我想从天数中扣除给定的假期列表。

4

1 回答 1

0

确保todayR_REL_DATE采用 pandas 日期时间格式pd.to_datetime()

import pandas as pd
import numpy as np
import datetime
df = pd.DataFrame({'R_REL_DATE': {0: '7/23/2020', 1: '8/26/2020'},
 'DAYS IN QUEUE': {0: 338, 1: 304}})
df["today"] = pd.to_datetime(datetime.date.today())
df["R_REL_DATE"] = pd.to_datetime(df["R_REL_DATE"])
start = df["R_REL_DATE"].values.astype('datetime64[D]')
end = df["today"].values.astype('datetime64[D]')
holiday = ['2021-06-19', '2021-06-20']
#Numpy function to find in between days
days = np.busday_count(start, end, weekmask='1111111', holidays=holiday)
#Add this column to dataframe
df["Days"] = days - 1
df
Out[1]: 
  R_REL_DATE  DAYS IN QUEUE      today  Days
0 2020-07-23            338 2021-06-27   336
1 2020-08-26            304 2021-06-27   302
于 2021-06-27T22:45:28.253 回答