使用以下日期时间数据,我想根据以下条件设置相对于“日期”的截止日期:
如果“日期”是在假日或周末,则截止日期应为下一个工作日的17:00。
如果“日期”是周一至周五,而不是假期,并且“日期”的时间在0-8之间,则截止日期应为同一工作日的17:00。
如果“日期”是周一至周五,而不是假期,并且“日期”的时间在9-17之间,则截止日期应为下一个非节假日,同时是工作日。
如果“日期”是周一至周五,而不是假期,并且“日期”的时间在18-23之间,则截止日期应为下一个非节假日,工作日为17:00
以下是数据:
import datetime
Holidays = [date(2018,1,1),date(2018,1,15),date(2018,2,19),date(2018,3,9)]
df = pd.DataFrame({'Date': ['2018-01-01 18:47','2018-01-08 06:11','2018-01-12 10:05','2018-02-10 09:22','2018-02-20 14:14','2018-03-08 16:17','2018-03-25 17:35'],
'Weekday': [0,0,4,5,1,3,6],
'Hour': [18,6,10,9,14,16,17]})
df['Date'] = pd.to_datetime(df['Date'])
结果应如下:
df = pd.DataFrame({'Date': ['2018-01-01 18:47','2018-01-08 06:11','2018-01-12 10:05','2018-02-10 09:22','2018-02-21 14:14','2018-03-08 16:17','2018-03-25 17:35'],
'Deadline': ['2018-01-02 17:00','2018-01-08 17:00','2018-01-16 10:05','2018-02-12 17:00','2018-02-22 14:14','2018-03-12 16:17','2018-03-26 17:00']})
答案 0 :(得分:1)
一一写下您的病情,然后我们使用np.select
from pandas.tseries.offsets import *
df.loc[df.Date.isin(Holidays),'WeekDay']=5#here I assign the holiday week to 5 in case we can consider the weekends and holiday in the same .
#make the conditions
con0=df.Weekday.isin([5,6]).values
con1=(df.Weekday.between(0,4)&df.Date.dt.hour.between(0,8)).values
con2=(df.Weekday.between(0,4)&df.Date.dt.hour.between(9,17)).values
con3=(df.Weekday.between(0,4)&df.Date.dt.hour.between(18,23)).values
# value to select
s=(df['Date'] + BDay())
s=pd.Series(np.where(s.dt.date.isin(Holidays),s+BDay(),s))# create your own bdays
r0=s.apply(lambda x : x.replace(hour=17,minute =0,second =0)).values
r1=df['Date'].apply(lambda x : x.replace(hour=17,minute =0,second =0)).values
r2=(s).values
r3=r0
# using np.select get the output
np.select([con0,con1,con2,con3],[r0,r1,r2,r3],default=np.datetime64('2005-02-25'))
Out[635]:
array(['2018-01-02T17:00:00.000000000', '2018-01-08T17:00:00.000000000',
'2018-01-16T10:05:00.000000000', '2018-02-12T17:00:00.000000000',
'2018-02-21T14:14:00.000000000', '2018-03-12T16:17:00.000000000',
'2018-03-26T17:00:00.000000000'], dtype='datetime64[ns]')