zoukankan      html  css  js  c++  java
  • Python获取一段日期内的工作日和所有日期

    Pandas

    1、获取工作日:bdate_range

    import pandas as pd
    e = pd.bdate_range('8/7/2019', '8/31/2019') e.date #获取日期列表
    array([datetime.date(2019, 8, 7), datetime.date(2019, 8, 8),
           datetime.date(2019, 8, 9), datetime.date(2019, 8, 12),
           datetime.date(2019, 8, 13), datetime.date(2019, 8, 14),
           datetime.date(2019, 8, 15), datetime.date(2019, 8, 16),
           datetime.date(2019, 8, 19), datetime.date(2019, 8, 20),
           datetime.date(2019, 8, 21), datetime.date(2019, 8, 22),
           datetime.date(2019, 8, 23), datetime.date(2019, 8, 26),
           datetime.date(2019, 8, 27), datetime.date(2019, 8, 28),
           datetime.date(2019, 8, 29), datetime.date(2019, 8, 30)],
          dtype=object)
    

    2、获取所有日期:date_range

    e = pd.date_range('8/7/2019', '8/31/2019')
    e.date
    array([datetime.date(2019, 8, 7), datetime.date(2019, 8, 8),
           datetime.date(2019, 8, 9), datetime.date(2019, 8, 10),
           datetime.date(2019, 8, 11), datetime.date(2019, 8, 12),
           datetime.date(2019, 8, 13), datetime.date(2019, 8, 14),
           datetime.date(2019, 8, 15), datetime.date(2019, 8, 16),
           datetime.date(2019, 8, 17), datetime.date(2019, 8, 18),
           datetime.date(2019, 8, 19), datetime.date(2019, 8, 20),
           datetime.date(2019, 8, 21), datetime.date(2019, 8, 22),
           datetime.date(2019, 8, 23), datetime.date(2019, 8, 24),
           datetime.date(2019, 8, 25), datetime.date(2019, 8, 26),
           datetime.date(2019, 8, 27), datetime.date(2019, 8, 28),
           datetime.date(2019, 8, 29), datetime.date(2019, 8, 30),
           datetime.date(2019, 8, 31)], dtype=object)
    

    3、如果需要过滤节日,请安装chinesecalendar 

    import datetime
    import chinese_calendar
    a = datetime.datetime.today()
    b = chinese_calendar.is_holiday(a)
    c = chinese_calendar.is_workday(a)
    print b, c
    # False True
    

      

  • 相关阅读:
    [Makefile] 递归编译的Makefile的实现
    python中出现 IndentationError:unindent does not match any outer indentation level
    Python3 编译中文字串报错解决方案
    linux下创建和删除软、硬链接
    免费的编程中文书籍索引
    starUML建立时序图
    UML 学习地址
    使用 Addr2line 将函数地址解析为函数名
    WIFI基本知识整理
    Ubuntu16.04 安装Python3.6 报错
  • 原文地址:https://www.cnblogs.com/qinghuaL/p/11316434.html
Copyright © 2011-2022 走看看