python 获取日期特征(工作日/周末/节假日等)

  • 需求:获取每天对应的日期特征——工作日、周末、节假日、指定节日(如618、双11、双12)
  • 业务场景:可以做不同日期类型下的流量、销量对比分析
  • 结果预示
    python 获取日期特征(工作日/周末/节假日等)_第1张图片
  • 主要运用的python库: chinese_calendar(需要每年初更新一下库)
  • python完整代码
import os
import time
import datetime
from datetime import timedelta
import pandas as pd
from chinese_calendar import is_holiday,get_holiday_detail

# 获取日期范围
def get_DateRange():
    start_time = datetime.datetime.strptime("2020-01-01","%Y-%m-%d")
    now = datetime.datetime.now()
    next_month_end = datetime.datetime(now.year, now.month + 2, 1) - timedelta(days=1)  # 下月最后一天
    date_range = pd.date_range(
        start=start_time,  # 开始日期
        end=next_month_end,  # 结束日期
        freq="D",
    )

    return pd.DataFrame(index=date_range)

# 判断节假日
def Get_is_holiday(date):
    '''
        获取节假日
    '''
    date = pd.to_datetime(date)
    IsHoliday = is_holiday(date)                      # 是否节假日
    OnHoliday, HolidayName = get_holiday_detail(date) # 是否节假日、节假日名称
    if IsHoliday == True:
        return HolidayName
    else:
        return "Work Day"

# 获取日期特征
def get_Feature_day(df):
    # 年、月、季度、星期
    df['year'] = [str(i.year) for i in df.index]  # 年
    df['month'] = [str(i.month) for i in df.index]  # 月
    df['quarter'] = [str((int(i) - 1) // 3 + 1) for i in df.month]  # 季度
    df['week'] = [str(i.isoweekday()) for i in df.index]  # 星期

    # 节假日、调休日
    list_ = [[i, Get_is_holiday(i)] for i in df.index]
    day_type = pd.DataFrame(list_, columns=['day', 'day_type'])
    df['day_type'] = day_type.day_type.fillna("Week Day & Change Holiday").tolist()

    df.day_type = ["双11" if i.day == 11 and m == "11" else t for i,m,t in zip(df.index,df.month,df.day_type)]     # 双12
    df.day_type = ["双12" if i.day == 12 and m == "12" else t for i, m, t in zip(df.index, df.month, df.day_type)] # 双11
    df.day_type = ["618" if i.day == 18 and m == "6" else t for i, m, t in zip(df.index, df.month, df.day_type)]   # 618
    df.day_type = ["818" if i.day == 18 and m == "8" else t for i, m, t in zip(df.index, df.month, df.day_type)]   # 818

    dict_day_type_chinese = {
        "New Year's Day":"元旦",
        "Spring Festival": "春节",
        "Tomb-sweeping Day": "清明节",
        "Labour Day": "劳动节",
        "Dragon Boat Festival": "端午节",
        "Mid-autumn Festival": "中秋节",
        "National Day": "国庆节",
        "Week Day & Change Holiday": "周末&调休",
        "Work Day": "工作日",
        "618": "618",
        "818": "818",
        "双11": "双11",
        "双12": "双12"
    }
    df['day_type_chinese'] = [dict_day_type_chinese[i] for i in df.day_type]

    columns = ['year', 'month', 'quarter', 'week', 'day_type','day_type_chinese']

    return df[columns]

if __name__ == "__main__":
    dir_ = os.getcwd()
    df_date_range = get_DateRange()
    feature_day = get_Feature_day(df_date_range)
    feature_day.to_excel("日期特征_{}.xlsx".format(datetime.datetime.now().strftime("%Y-%m-%d")))
    print("程序运行完成")
    time.sleep(10)

你可能感兴趣的:(python,pandas,开发语言,数据分析,数据挖掘)