3 回答
TA贡献1826条经验 获得超6个赞
Pandas 支持通过自定义工作日提供您自己的假期。
该解决方案的好处是无缝支持相邻的假期;例如,某些地区的节礼日和圣诞节。
# define custom business days
weekmask = 'Mon Tue Wed Thu Fri'
holidays = ['2018-10-11']
bday = pd.tseries.offsets.CustomBusinessDay(holidays=holidays, weekmask=weekmask)
# construct mask to identify when days must be sutracted
m1 = df['category'] == 'B'
m2 = df['day'].dt.weekday.isin([5, 6]) | df['day'].isin(holidays)
# apply conditional logic
df['day'] = np.where(m1 & m2, df['day'] - bday, df['day'])
print(df)
category day
0 A 2018-10-10
1 B 2018-10-10
2 C 2018-10-12
3 B 2018-10-12
4 C 2018-10-14
5 A 2018-10-15
编辑:根据您的评论,“我刚刚意识到我没有问清楚我想要什么。我想找到前一个工作日”,您可以简单地使用:
df['day'] -= bday
添加回答
举报