业务中经常需要去分析时间序列相关的数据,可能会用到年,月,日,小时,星期,节假日等等,尤其是在零售和电商行业中,可能需要分析日销售的高峰时段等等,所以能够有这样一张多维度细颗粒的时间表其实用起来是非常简便的,话不多说,直接贴代码
import pandas as pd
from datetime import datetime,timedelta
from chinese_calendar import is_holiday,is_workday
starttime = '2019-01-01'
n = 730
date_list = []
year_list = []
month_list = []
day_list = []
weeknum_list = []
week_list = []
is_week = []
quarter = []
is_work = []
holiday = []
if type(starttime) == type('rrr'):
a = datetime.strptime(starttime,'%Y-%m-%d').date()
else:
a = starttime
for i in range(n):
day_ = a+timedelta(days=i)
date_list.append(day_.strftime("%Y-%m-%d"))
year_list.append(day_.year)
month_list.append(day_.month)
day_list.append(day_.day)
weeknum_list.append(day_.strftime("%A"))
week_list.append("|