欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

python实现判断某天是否是节假日

程序员文章站 2022-03-03 10:01:05
...
import pandas as pd

from datetime import datetime
import pandas as pd

startdate = '2016-01-01'
enddate = '2016-12-31'
holiday = ['01-01', '05-01']

datalist = list(pd.date_range(start=startdate, end=enddate))
df = pd.DataFrame({'date':datalist})
print("df:\n",df)

df['month'] = df['date'].apply(lambda x: x.month)
df['day'] = df['date'].apply(lambda x: x.day)
df['weekday'] = df['date'].apply(lambda x: x.weekday()+1)
print("df:\n",df)

#工作日与休息日
isrest = ((df['weekday'] == 6) | (df['weekday'] == 7))
print("isrest:\n",isrest)
df['label'] = isrest*1 #布尔值转数值
print("df['label']:\n",df['label'])
print("df:\n",df)
#节假日
for h in holiday:
    #h = '01-01'
    h_month,h_day = h.split('-')
    print("h_month,h_day:\n",h_month,h_day)
    h_index = ((df['month'] == int(h_month)) & (df['day'] == int(h_day)))
    print("h_index:\n",h_index)
    df.loc[h_index, 'label'] = 2
print("df:\n",df)

参考:https://bbs.pinggu.org/thread-5980288-1-1.html

相关标签: python