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:小时制,基准 ...
随机推荐
- Nginx里的root/index/alias/proxy_pass的意思
1.[alias] 别名配置,用于访问文件系统,在匹配到location配置的URL路径后,指向[alias]配置的路径.如: location /test/ { alias /home/sftp/i ...
- Mongodb索引实战
最近碰到这样的一个需求,一张酒店政策优惠表,我们要根据用户入住和离开的时间,计算一家酒店的最低价政策前10位,数据库表字段如下: 'hid':88, 酒店id 'date':20150530, 入住日 ...
- centos 用户组操作
adduser testuser //新建testuser 用户 passwd testuser //给testuser 用户设置密码 useradd -g testgroup testuser // ...
- 使用Lombok总结
Lombok学习总结 Project Lombok is a java library that automatically plugs into your editor and build tool ...
- (零)linux 学习 -- 从 shell 开始
The Linux Command Line 读书笔记 - 部分内容来自 http://billie66.github.io/TLCL/book/chap02.html 文章目录 前言 什么是 she ...
- (一)Spring Security Demo 登陆与退出
文章目录 配置springSecurityFilterChain过滤器 配置身份验证 加载配置 登陆项目 退出 下面的代码需要spring环境的支持: 看这个系列博客之前,需要这个博客,大概了解下 s ...
- C++利用权重方法将二进制正数转换为十进制数
#include <iostream> #include <Windows.h> #include <string> using namespace std; in ...
- WUSTOJ 1338: The minimum square sum(Java)
题目链接:1338: The minimum square sum Description Given a prime p(p<108), you are to find min{x2+y2}, ...
- Mybatis整合(Redis、Ehcache)实现二级缓存
目的: Mybatis整合Ehcache实现二级缓存 Mybatis整合Redis实现二级缓存 Mybatis整合ehcache实现二级缓存 ssm中整合ehcache 在POM中导入相关依赖 < ...
- 在论坛中出现的比较难的sql问题:28(循环查询表来实现递归)
原文:在论坛中出现的比较难的sql问题:28(循环查询表来实现递归) 最近,在论坛中,遇到了不少比较难的sql问题,虽然自己都能解决,但发现过几天后,就记不起来了,也忘记解决的方法了. 所以,觉得有必 ...