def day_type()

in fclib/fclib/feature_engineering/feature_utils.py [0:0]


def day_type(datetime_col, holiday_col=None, semi_holiday_offset=timedelta(days=1)):
    """
    Convert datetime_col to 7 day types
    0: Monday
    2: Tuesday, Wednesday, and Thursday
    4: Friday
    5: Saturday
    6: Sunday
    7: Holiday
    8: Days before and after a holiday

    Args:
        datetime_col: Datetime column.
        holiday_col: Holiday code column. Default value None.
        semi_holiday_offset: Time difference between the date before (or after)
            the holiday and the holiday. Default value timedelta(days=1).
    
    Returns:
        A numpy array containing converted datatime_col into day types.
    """

    datetype = pd.DataFrame({"DayType": datetime_col.dt.dayofweek})
    datetype.replace({"DayType": WEEK_DAY_TYPE_MAP}, inplace=True)

    if holiday_col is not None:
        holiday_mask = holiday_col > 0
        datetype.loc[holiday_mask, "DayType"] = HOLIDAY_CODE

        # Create a temporary Date column to calculate dates near the holidays
        datetype["Date"] = pd.to_datetime(datetime_col.dt.date, format=DATETIME_FORMAT)
        holiday_dates = set(datetype.loc[holiday_mask, "Date"])

        semi_holiday_dates = [
            pd.date_range(start=d - semi_holiday_offset, end=d + semi_holiday_offset, freq="D") for d in holiday_dates
        ]

        # Flatten the list of lists
        semi_holiday_dates = [d for dates in semi_holiday_dates for d in dates]

        semi_holiday_dates = set(semi_holiday_dates)
        semi_holiday_dates = semi_holiday_dates.difference(holiday_dates)

        datetype.loc[datetype["Date"].isin(semi_holiday_dates), "DayType"] = SEMI_HOLIDAY_CODE

    return datetype["DayType"].values