2020年日期表-python实现
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实现的更多相关文章
- 一起学微软Power BI系列-使用技巧(5)自定义PowerBI时间日期表
1.日期函数表作用 经常使用Excel或者PowerBI,Power Pivot做报表,时间日期是一个重要的纬度,加上做一些钻取,时间日期函数表不可避免.所以今天就给大家分享一个自定义的做日期表的方法 ...
- DAX和Power BI中的参考日期表
本文使用Power BI模板描述DAX中的引用Date表,可以在Analysis Services模型中使用相同的技术.在Dax Date Template页面下载最新版本的模板. 为什么引用Date ...
- sql生成一个日期表
SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGO-- =============================================-- Auth ...
- MySQL 如何生成日期表
MySQL 如何生成日期表 在开发过程中,经常会遇到统计问题,通常交易信息都不是连续的,此时,统计出来的数据都是不连续的,所以提前生成一个时期表,当没有交易数据的时候填充0,就可以了,下面是生成日期表 ...
- PowerBI 应用时间智能(生成日期表)
简介 Power BI Desktop -是一款由微软发布的自助式商业智能工具,功能强大.易于使用.其中还可以通过微软云连多个数据源并且使用数据源来创建可视化表盘. 但是几乎所有的BI都需要展示如何随 ...
- 2020年秋季最新Python详细入门教程!全网最新最全
1. import # -*- coding: utf-8 -*- ## 引入新的包 import turtle import pickle # 文件操作 import tensorflow as t ...
- 如何从日期对象python获取以毫秒(秒后3位小数)为单位的时间值?
要在python中,要获取具有毫秒(秒后3位小数)的日期字符串,请使用以下命令: %f 显示毫秒 import datetime # 获得当前时间 now=datetime.datetime.now( ...
- Arrow-一个最好用的日期时间Python处理库
https://www.jianshu.com/p/c878bb1c48c1 写过Python程序的人大都知道,Python日期和时间的处理非常繁琐和麻烦,主要有以下几个问题: 有众多的package ...
- MATLAB datenum日期转换为Python日期
摘要 MATLAB datenum时间格式参数众多,本文只简单关注 units 参数,即基准年份和计时度量(天.小时). 命令行演示在 ipython 和 Octave 中进行. 示例1:小时制,基准 ...
随机推荐
- orleans 的一种模式
为了避免过热的grain,按时间%cpu数,分派任务到grain中,然后有限制的去访问原来过热的grain.eg:tokengrain,1个半小时后,更新所有的grain.
- unix 命令
ubuntu 命令窗口的打开 打开命令行窗口: Ctrl+Alt+T 在打开的命令行窗口中打开一个新的Tab: Ctrl+Shift+T 在同一窗口的Tab间切换: Ctrl+Page Up 或者 ...
- C#、.NET、ASP.NET之间的关系
一.前言 这是个人的笔记,在博客园已经有不少大佬已经写过了.但我自己就想留点笔记在属于我自己的博客.所以大佬忽略就行,不喜勿碰.谢谢!!! 二.个人笔记 C# 全称(C sharp),它是微软公司发布 ...
- PHP使用glob方法遍历文件夹下所有文件
PHP使用glob方法遍历文件夹下所有文件 遍历文件夹下所有文件,一般可以使用opendir 与 readdir 方法来遍历.<pre><?php$path = dirname(__ ...
- 教你使用Webpack搭建环境 TypeScript (2)
一. 环境搭建1.1. TypeScript环境安装已经配置好的环境,大家可以直接下载:https://github.com/coderwhy/HYLearnTS.git在上一个章节中我们说过,T ...
- java多线程中篇(三) —— 线程的控制(创建,运行,阻塞,中断,结束)
简介 线程的控制就是程序对线程的主要管理,最重要的就是状态的切换维护. 每种转态都有不同的引发事件(对应线程的方法),每种状态又有各自不同的处理步骤和过程,整个线程控制主要就是涉及这些内容. 正文 线 ...
- Zuul【自定义Filter】
实际业务中,如果要自定义filter过滤器,只需集成ZuulFIlter类即可,该类是个抽象类,它实现了IZuulFIlter接口,我们需要实现几个方法,如下示例: import static org ...
- 07 IO流(四)——文件字节流 FileInputStream/FileOutputStream与文件的拷贝
两个类的简述 专门用来对文件进行读写的类. 父类是InputStream.OutputStream 文件读入细节 FileOutputStream流的构造方法:new FileOutputStream ...
- UNIX环境高级编程笔记 目录
每一章的重点会使用加粗字体 第一章:UNIX基础知识:UNIX体系结构:文件和目录:输入和输出:程序和进程:出错处理:信号:时间值:系统调用和库函数 第三章:文件I/O:文件描述符:文件操作函数:文件 ...
- Vue基础语法(样式绑定,事件处理,表单,Vue组件)
样式绑定 事件处理 表单 Vue组件 样式绑定 <!DOCTYPE html> <html> <head> <meta charset="utf-8 ...