---
id: "57002387-5ccc-468a-8c4f-ece18bf81866"
name: polars-mstl-decomposition-data-preparation-d4310dcda97f
description: "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."
version: "0.1.0"
tags:
  - "polars"
  - "statsforecast"
  - "mstl"
  - "time-series"
  - "decomposition"
  - "feature-engineering"
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"
---

# 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


---
Source attribution: ECNU-ICALK
Original project: https://github.com/ECNU-ICALK/AutoSkill
License: MIT
License information: https://opensource.org/license/mit
Original upstream instructions; downloaded from Mega AI Hub.

## Upstream license notice

MIT License

Copyright (c) 2026 Gabriel Moreira

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.



# Mega AI Hub resource metadata

Creator / publisher: ECNU-ICALK
Original project: https://github.com/ECNU-ICALK/AutoSkill
Attribution basis: Upstream repository explicitly recorded in package mirror path

Fields of work: Data & Analytics, Development & Engineering
Roles: Data Analyst, Data Scientist, ML / AI Engineer
Specialties: API, Database
Tools: Polars, Statsforecast
Country applicability: Country not specified
Classification basis: Source catalog labels
Instruction SHA-256: 58873226d3cad633da5d5e31c86f1574853fbb6da321f2a655fd227eb2b67601
