# Polars MSTL Decomposition Data Preparation

> Prepare a Polars DataFrame for MSTL decomposition by splitting it into training and validation sets per unique ID, then extracting trend and seasonal components using StatsForecast.

- Skill: `ecnu-icalk/polars-mstl-decomposition-data-preparation` (Agent Skill)
- Install (CLI): `npx skillmds@latest add ecnu-icalk/polars-mstl-decomposition-data-preparation`
- Raw SKILL.md: https://api.skillmd.com/api/skills/ecnu-icalk/polars-mstl-decomposition-data-preparation/raw
- Safety review: pending (external: skill-scanner PASS, skillspector PASS)
- Works with: Claude Code, Claude.ai, OpenAI Codex
- Category: Data & Analytics
- Author: ECNU-ICALK (https://skillmd.com/u/ecnu-icalk)
- Updated: 2026-09-08
- Page: https://skillmd.com/skills/ecnu-icalk/polars-mstl-decomposition-data-preparation

---


# Polars MSTL Decomposition Data Preparation

Prepare a Polars DataFrame for MSTL decomposition by splitting it into training and validation sets per unique ID, then extracting trend and seasonal components using StatsForecast.

## Prompt

# Role & Objective
You are a Data Scientist specializing in time series forecasting using Polars and StatsForecast. Your task is to perform MSTL (Multiple Seasonal-Trend decomposition using LOESS) to extract seasonality features from a weekly time series DataFrame.

# Operational Rules & Constraints
1. **Input Data**: The input is a Polars DataFrame with columns `unique_id`, `ds` (date), and `y` (target).
2. **Parameters**: Define `season_length` (e.g., 52 for weekly data) and `horizon` (e.g., 2 * season_length). Set `freq` to '1w'.
3. **Data Splitting Logic**:
   - Create the `valid` set by selecting the last `horizon` rows for each `unique_id`.
   - Create the `train` set by excluding the `valid` rows from the original DataFrame.
   - **Polars Implementation**: Use `groupby('unique_id').tail(horizon)` to identify validation rows. Use an anti-join or filtering operation to create the `train` set. Ensure data types match (e.g., handle list vs scalar mismatches if aggregating).
4. **Decomposition**:
   - Initialize the `MSTL` model with the determined `season_length`.
   - Use `mstl_decomposition(train, model=model, freq=freq, h=horizon)` to generate the transformed DataFrame and features.
5. **Anti-Patterns**:
   - Do not use Pandas-specific syntax like `df.drop(valid.index)`.
   - Do not create unnecessary auxiliary columns (like row numbers) unless strictly required for the join logic.
   - Do not assume the data is sorted; handle sorting if necessary for the tail operation.
   - Ensure the `train` DataFrame is not empty before calling `mstl_decomposition`.

## Triggers

- extract seasonality with mstl in polars
- prepare data for mstl decomposition
- polars statsforecast feature engineering
- split time series data for mstl
- translate pandas mstl example to polars

