import pandas as pd
import calendar
import datetime
# 生成日期范围
date = pd.date_range("2020-01-01","2020-12-31",freq="D")
# 将日期转化为字符串
dt_str = [i.strftime("%Y%m%d") for i in date]
# 日期对应的星期(数值)
week = [int(i.strftime("%w")) for i in date] # 0表示星期日
# 日期对应的星期(中文)
week_desc = []
def week_desc_info():
for i in week:
if i == 0:
week_desc.append("星期天")
elif i == 1:
week_desc.append("星期一")
elif i == 2:
week_desc.append("星期二")
elif i == 3:
week_desc.append("星期三")
elif i == 4:
week_desc.append("星期四")
elif i == 5:
week_desc.append("星期五")
elif i == 6:
week_desc.append("星期六")
return week_desc
week_desc = week_desc_info()
# 月份(数值,01,02,...)
month = [i.strftime("%m") for i in date]
# 月份(中文)
month_desc = [str(i) + "月" for i in month]
# 季度(数值)
season = []
def season_desc_info():
for i in list(int(j) for j in month):
if i <= 3:
season.append(1)
elif i > 3 and i <= 6:
season.append(2)
elif i > 6 and i <= 9:
season.append(3)
elif i > 9 and i <= 12:
season.append(4)
return season
season = season_desc_info()
# 季度(中文)
season_desc = ["第" + str(i) + "季度" for i in season]
# 年份(数值)
year = [int(i.strftime("%Y")) for i in date]
# 年份(中文)
year_desc = [ str(i) + "年" for i in year]
# 一年中的第几天
date_seq = [int(i.strftime("%j")) for i in date]
# 一年中的第几周
week_seq = [int(i.strftime("%U")) for i in date]
# 是否周末
weekend_flag = []
def is_weekend_flag():
for i in week:
if i == 0 or i == 6:
weekend_flag.append(1)
else:
weekend_flag.append(0)
return weekend_flag
weekend_flag = is_weekend_flag()
# 是否月末
monend_flag = []
def is_month_lastday():
for i in date:
#获得当月一共有多少天(也就是最后一天的日期)
_,days_in_month = calendar.monthrange(i.year, i.month)
# 获取当天是当月中的第几天
day_of_month =int(i.strftime("%d"))
if day_of_month == days_in_month:
monend_flag.append(1)
else:
monend_flag.append(0)
return monend_flag
monend_flag = is_month_lastday()
# 节假日标识
holiday_tp = []
# 节假日名称
holiday_desc = []
def is_holiday_desc():
for i in dt_str:
if i == '20200101':
holiday_desc.append("元旦")
elif i in ('20200124','20200125','20200126','20200127','20200128','20200129','20200130'):
holiday_desc.append("春节")
elif i in ('20200404','20200405','20200406'):
holiday_desc.append("清明节")
elif i in ('20200501','20200502','20200503','20200504','20200505'):
holiday_desc.append("国际劳动节")
elif i in ('20200625','20200626','20200627'):
holiday_desc.append("端午节")
elif i in ('20201001'):
holiday_desc.append("中秋节/国庆节")
elif i in ('20201002','20201003','20201004','20201005','20201006','20201007','20201008'):
holiday_desc.append("国庆节")
else:
holiday_desc.append("非节假日")
return holiday_desc
holiday_desc = is_holiday_desc()
# 周末标识
weekend_desc = []
def is_weekend_desc():
for i in date:
date_str = i.strftime("%Y%m%d")
if date_str in ('20200119','20200201','20200426','20200509','20200628','20200927','20201010'):
weekend_desc.append("非周末")
else:
week_id = int(i.strftime("%w"))
if week_id in (0,6):
weekend_desc.append("周末")
else:
weekend_desc.append("非周末")
return weekend_desc
weekend_desc = is_weekend_desc()
# 月末标识
monend_desc = []
def is_monend_desc():
for i in monend_flag:
if i == 1:
monend_desc.append("月末")
else:
monend_desc.append("非月末")
return monend_desc
monend_desc = is_monend_desc()
# 旬(数值)
xun_id = []
def get_xun_id():
for i in date:
# 获取当天是当月中的第几天
day_of_month =int(i.strftime("%d"))
if day_of_month <= 10:
xun_id.append(1)
elif day_of_month > 10 and day_of_month <= 20:
xun_id.append(2)
else:
xun_id.append(3)
return xun_id
xun_id = get_xun_id()
# 旬(中文)
xun_desc = []
def get_xun_desc():
for i in xun_id:
if i == 1:
xun_desc.append("上旬")
elif i == 2:
xun_desc.append("中旬")
else:
xun_desc.append("下旬")
return xun_desc
xun_desc = get_xun_desc()
# 周(数值)
week_seq_id = []
def get_week_seq_id():
for i in date:
current_week = 0
# 构造当前月的1号
first_day_of_month = datetime.date(i.year, i.month, 1);
#获得当月第一天是星期几,并获取当月一共有多少天(也就是最后一天的日期)
week,days_in_month = calendar.monthrange(first_day_of_month.year, first_day_of_month.month)
#用来存储,当第一天不是星期天时,这个星期还有多少天
if week > 0:
t1 = 7 - week
else:
t1 = 0
# 获取当天是当月中的第几天
t2 = (int(i.strftime("%d")) - t1) % 7
t3 = 0
if t2 != 0:
t3 = (int(i.strftime("%d")) - t1) // 7 + 2
else:
t3 = (int(i.strftime("%d")) - t1) // 7 + 1
current_week = current_week + t3
week_seq_id.append(current_week)
return week_seq_id
week_seq_id=get_week_seq_id()
# 周(中文)
week_seq_desc = []
def get_week_seq_desc():
for i in week_seq_id:
if i == 1:
week_seq_desc.append("第一周")
elif i == 2:
week_seq_desc.append("第二周")
elif i == 3:
week_seq_desc.append("第三周")
elif i == 4:
week_seq_desc.append("第四周")
elif i == 5:
week_seq_desc.append("第五周")
elif i == 6:
week_seq_desc.append("第六周")
else:
week_seq_desc.append("第七周")
return week_seq_desc
week_seq_desc = get_week_seq_desc()
# 构造日期维表
dataframe = pd.DataFrame({'date':dt_str,'week':week,'week_desc':week_desc,\
'month':month,'month_desc':month_desc,\
'season':season,'season_desc':season_desc,\
'year':year,'year_desc':year_desc,\
'date_seq':date_seq,'week_seq':week_seq,\
'weekend_flag':weekend_flag,'monend_flag':monend_flag,\
'holiday_desc':holiday_desc,\
'weekend_desc':weekend_desc,'monend_desc':monend_desc,\
'xun_id':xun_id,'xun_desc':xun_desc,\
'week_seq_id':week_seq_id,'week_seq_desc':week_seq_desc})
dataframe.to_excel('./data/date2020.xlsx',index=False)