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)

2020年日期表-python实现的更多相关文章

  1. 一起学微软Power BI系列-使用技巧(5)自定义PowerBI时间日期表

    1.日期函数表作用 经常使用Excel或者PowerBI,Power Pivot做报表,时间日期是一个重要的纬度,加上做一些钻取,时间日期函数表不可避免.所以今天就给大家分享一个自定义的做日期表的方法 ...

  2. DAX和Power BI中的参考日期表

    本文使用Power BI模板描述DAX中的引用Date表,可以在Analysis Services模型中使用相同的技术.在Dax Date Template页面下载最新版本的模板. 为什么引用Date ...

  3. sql生成一个日期表

    SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGO-- =============================================-- Auth ...

  4. MySQL 如何生成日期表

    MySQL 如何生成日期表 在开发过程中,经常会遇到统计问题,通常交易信息都不是连续的,此时,统计出来的数据都是不连续的,所以提前生成一个时期表,当没有交易数据的时候填充0,就可以了,下面是生成日期表 ...

  5. PowerBI 应用时间智能(生成日期表)

    简介 Power BI Desktop -是一款由微软发布的自助式商业智能工具,功能强大.易于使用.其中还可以通过微软云连多个数据源并且使用数据源来创建可视化表盘. 但是几乎所有的BI都需要展示如何随 ...

  6. 2020年秋季最新Python详细入门教程!全网最新最全

    1. import # -*- coding: utf-8 -*- ## 引入新的包 import turtle import pickle # 文件操作 import tensorflow as t ...

  7. 如何从日期对象python获取以毫秒(秒后3位小数)为单位的时间值?

    要在python中,要获取具有毫秒(秒后3位小数)的日期字符串,请使用以下命令: %f 显示毫秒 import datetime # 获得当前时间 now=datetime.datetime.now( ...

  8. Arrow-一个最好用的日期时间Python处理库

    https://www.jianshu.com/p/c878bb1c48c1 写过Python程序的人大都知道,Python日期和时间的处理非常繁琐和麻烦,主要有以下几个问题: 有众多的package ...

  9. MATLAB datenum日期转换为Python日期

    摘要 MATLAB datenum时间格式参数众多,本文只简单关注 units 参数,即基准年份和计时度量(天.小时). 命令行演示在 ipython 和 Octave 中进行. 示例1:小时制,基准 ...

随机推荐

  1. 设计模式之--Visitor

    Ref: https://www.jianshu.com/p/feec47a25b67 https://www.cnblogs.com/alphablox/p/5346567.html

  2. [bzoj4345][POI2016]Korale_堆_贪心_线段树_dfs

    bzoj4345 POI2016 Korale 题目链接:https://lydsy.com/JudgeOnline/problem.php?id=4345 数据范围:略. 题解: 由于$k$的范围问 ...

  3. tp5功能模块添加与调试

    在原先完善的功能基础上添加比如导出列表为excel ,一下子把所有属性写全了,出了问题,不好查找问题在哪? 所以遇到这种问题,需要最简单的测试.比如新建一个mysql表内就放一列一行数据.减少代码量, ...

  4. python基础学习(八)

    17.嵌套循环 # 嵌套循环 nested loop # 在一个循环中使用另外一个循环 num_list1 = [1, 2, 3, 4] num_list2 = [6, 7, 8, 9] # 组合li ...

  5. Hystrix【异常机制处理】

    在之前的老版本中,feign中是默认开启hystrix的,从新版本中默认已经关闭了,如果要通过FeignClient调用服务并开启hystrix的话,需要自定义开启,即:feign.hystrix.e ...

  6. C++ 根据两点式方法求直线并求两条直线的交点

    Line.h #pragma once //Microsoft Visual Studio 2015 Enterprise //根据两点式方法求直线,并求两条直线的交点 #include"B ...

  7. Python27之集合

    集合说:“在我的世界里,你就是唯一” 一.集合的概念和使用 集合的概念和数学里数学里集合的概念是一致的,都是一组元素的集,且元素之间不能重复.元素必须是不可变的数据类型,例如元组也可以作为其中的一个元 ...

  8. PHP中的PDO数据对象

    PDO: PHP Data Object:php的数据对象.pdo是数据库操作工具类!1,它能操作很多种数据库,比如mysql,oracle,sybase....2,它具有操作数据库的更多的功能,比如 ...

  9. OpenCV学习笔记3

    OpenCV学习笔记3 图像平滑(低通滤波) 使用低通滤波器可以达到图像模糊的目的.这对与去除噪音很有帮助.其实就是去除图像中的高频成分(比如:噪音,边界).所以边界也会被模糊一点.(当然,也有一些模 ...

  10. 【转载】使用Jedis操作redis

    Redis是一个开源的Key-Value数据缓存,和Memcached类似. Redis多种类型的value,包括string(字符串).list(链表).set(集合).zset(sorted se ...