utils.py
utils.py
This module contains helper functions for the EDA and modelling
exercises. Feel free to use them to get you started more quickly.
"""
import pandas as pd
import numpy as np
from datetime import datetime
def parse_time(s):
return datetime.strptime(s, "%Y-%m-%d").date()
def read_demand(path):
df = pd.read_csv(path)
df = df.assign(date=lambda df: df.date.apply(parse_time))
df = df.set_index("date")
df.index = pd.DatetimeIndex(df.index)
return df
def read_promotions(path):
df = pd.read_csv(path, index_col=0)
df = df.assign(promotion_date=lambda df:
df.promotion_date.apply(parse_time))
df = df.set_index("promotion_date")
df.index = pd.DatetimeIndex(df.index)
return df
def aggregate_to_weekly(df):
grouped = df.groupby(["sku", "supermarket"])
# Performs a simplistic aggregation of promotion. If a promotion
occured during the week this variable will be true.
weekly = grouped.apply(lambda df: df.resample("W").agg({"demand":
"sum", "promotion": "max"}))
weekly = weekly.reset_index().set_index("date")
return weekly