EDA and Modelling Helper Functions
EDA and Modelling Helper Functions
Setting the date as an index and converting it into a DateTimeIndex is crucial as it optimizes the DataFrame for time series operations, facilitating efficient resampling, rolling aggregations, and time-based indexing and slicing. This enhances performance and ease in temporal analyses.
The 'read_demand' function reads a CSV file into a DataFrame, transforms the 'date' column to a datetime format using the 'parse_time' function, sets this column as the index, and ensures the index is a DateTimeIndex. This prepares the DataFrame for time series operations.
The 'aggregate_to_weekly' function groups the DataFrame by 'sku' and 'supermarket', then resamples the data to weekly frequency. It sums the 'demand' and takes the maximum of 'promotion' within each week. This transformation allows for analysis on a broader temporal scale, smoothing daily fluctuations while highlighting weeks with any promotions.
The 'clean_demand_per_group' function methodically applies the cleaning process to individual groups formed by unique combinations of supermarkets and SKUs. This ensures that each group retains its intrinsic demand characteristics while missing data is cleaned group-specifically to preserve the unique variance and demand patterns intrinsic to each group.
The 'merge' function integrates demand and promotion DataFrames by performing an outer join on 'supermarket', 'sku', and 'date', ensuring that all entries are included regardless of a match. Missing promotion values are then set to False, indicating no promotion occurred, thereby maintaining data integrity while incorporating promotional context.
The 'extend_promotions_days' function duplicates each promotion entry for a specified number of days by adding new rows for each day a promotion lasts, incremented by days using Timedelta. 'Promotion_id' serves as a unique identifier for each promotion event, ensuring traceability of these extended entries across the time span of the promotion.
The 'clean' function aims to address missing values in a time series. It first fills any gaps by propagating the last valid observation forward to the next valid observation (backfill), and any remaining missing values are replaced with the series' mean. This approach balances preserving trends and filling gaps effectively.
Using backfill and mean filling can smooth out short-term data volatility and fill in gaps which might bias prediction models by diluting pronounced patterns in the data. This might impact the model's sensitivity to detect swift changes or anomalies essential in demand forecasting or anomaly detection tasks.
Resetting the index in 'aggregate_to_weekly' ensures that 'date' becomes an explicit column instead of an index after aggregating by week. This restores the DataFrame structure for further operations or merges where 'date' as a linearly accessible column is crucial.
Extending promotion days simulates the ongoing impact of a promotion beyond its initial day, potentially capturing the extended influence on demand patterns across time. This can lead to models that better understand lagged effects of promotions, allowing for more accurate prediction of sustained increases in demand beyond promotional periods.