Python获取一段日期内的工作日和所有日期
Pandas
1、获取工作日:bdate_range
import pandas as pd
e = pd.bdate_range('8/7/2019', '8/31/2019')
e.date #获取日期列表
array([datetime.date(2019, 8, 7), datetime.date(2019, 8, 8),
datetime.date(2019, 8, 9), datetime.date(2019, 8, 12),
datetime.date(2019, 8, 13), datetime.date(2019, 8, 14),
datetime.date(2019, 8, 15), datetime.date(2019, 8, 16),
datetime.date(2019, 8, 19), datetime.date(2019, 8, 20),
datetime.date(2019, 8, 21), datetime.date(2019, 8, 22),
datetime.date(2019, 8, 23), datetime.date(2019, 8, 26),
datetime.date(2019, 8, 27), datetime.date(2019, 8, 28),
datetime.date(2019, 8, 29), datetime.date(2019, 8, 30)],
dtype=object)
2、获取所有日期:date_range
e = pd.date_range('8/7/2019', '8/31/2019')
e.date
array([datetime.date(2019, 8, 7), datetime.date(2019, 8, 8),
datetime.date(2019, 8, 9), datetime.date(2019, 8, 10),
datetime.date(2019, 8, 11), datetime.date(2019, 8, 12),
datetime.date(2019, 8, 13), datetime.date(2019, 8, 14),
datetime.date(2019, 8, 15), datetime.date(2019, 8, 16),
datetime.date(2019, 8, 17), datetime.date(2019, 8, 18),
datetime.date(2019, 8, 19), datetime.date(2019, 8, 20),
datetime.date(2019, 8, 21), datetime.date(2019, 8, 22),
datetime.date(2019, 8, 23), datetime.date(2019, 8, 24),
datetime.date(2019, 8, 25), datetime.date(2019, 8, 26),
datetime.date(2019, 8, 27), datetime.date(2019, 8, 28),
datetime.date(2019, 8, 29), datetime.date(2019, 8, 30),
datetime.date(2019, 8, 31)], dtype=object)
3、如果需要过滤节日,请安装chinesecalendar
import datetime
import chinese_calendar
a = datetime.datetime.today()
b = chinese_calendar.is_holiday(a)
c = chinese_calendar.is_workday(a)
print b, c
# False True