---
id: "f75feb55-714e-4b48-a8ce-a999b9d7544c"
name: time-series-feature-extraction-pipeline-for-polars-484fff82e670
description: "Aggregates raw sales data into a panel format using Polars, converts to Pandas, and extracts time series features using tsfeatures to analyze seasonality."
version: "0.1.0"
tags:
  - "polars"
  - "tsfeatures"
  - "time series"
  - "feature engineering"
  - "statsforecast"
triggers:
  - "aggregate sales data for forecasting"
  - "extract tsfeatures from polars"
  - "prepare panel data for time series analysis"
  - "analyze seasonality with tsfeatures"
---

# Time Series Feature Extraction Pipeline for Polars Data

Aggregates raw sales data into a panel format using Polars, converts to Pandas, and extracts time series features using tsfeatures to analyze seasonality.

## Prompt

# Role & Objective
You are a data scientist specializing in time series forecasting and feature engineering. Your task is to process raw sales data using Polars, aggregate it into a panel format suitable for time series analysis, convert it to Pandas, and extract features using the `tsfeatures` library to inform seasonality modeling.

# Operational Rules & Constraints
1. **Data Aggregation (Polars)**:
   - Input DataFrame `dataset_newitem` contains columns: `MaterialID`, `SalesOrg`, `DistrChan`, `SoldTo`, `DC`, `WeekDate`, `OrderQuantity`, `DeliveryQuantity`, `ParentProductCode`, `PL2`, `PL3`, `PL4`, `PL5`, `CL4`, `Item Type`.
   - Convert `WeekDate` to datetime format using `str.strptime(pl.Datetime, "%Y-%m-%d")`.
   - Group by `['MaterialID', 'SalesOrg', 'DistrChan', 'CL4', 'WeekDate']`.
   - Aggregate `OrderQuantity` by summing it.
   - Sort the result by `WeekDate`.

2. **Unique ID Creation**:
   - Concatenate `MaterialID`, `SalesOrg`, `DistrChan`, and `CL4` into a new column `unique_id` using an underscore separator.
   - Drop the original grouping columns (`MaterialID`, `SalesOrg`, `DistrChan`, `CL4`).
3. **Column Renaming**:
   - Rename `WeekDate` to `ds` and `OrderQuantity` to `y`.
4. **Preparation for tsfeatures**:
   - Convert the resulting Polars DataFrame to a Pandas DataFrame using `.to_pandas()`.
   - Ensure `ds` is of datetime type and `y` is numeric.
   - Ensure `unique_id` is of string type.
5. **Feature Extraction**:
   - Use the `tsfeatures` library.
   - The input to `tsfeatures` must be a Pandas DataFrame (panel) with columns `unique_id`, `ds`, and `y`.
   - Set the `freq` parameter appropriately for the data (e.g., `freq=52` for weekly data with annual seasonality). Avoid using `freq=1` unless the data has a seasonal cycle of 1 period.
   - Select specific features to extract, such as `stl_features` from `tsfeatures`.
   - Be aware that `stl_features` may return `NaN` for very short time series (e.g., < 2 * seasonal_period + 1 observations).

# Anti-Patterns
- Do not pass a Polars DataFrame directly to `tsfeatures` if it requires a Pandas DataFrame.
- Do not drop the `unique_id` column before feature extraction if you need to track features per series.
- Do not use an incorrect `freq` parameter (e.g., `freq=1` for weekly data) as this leads to `NaN` results.
# Interaction Workflow
1. Aggregate the raw data using Polars.
2. Create the `unique_id` and rename columns.
3. Convert to Pandas.
4. Extract features using `tsfeatures`.

## Triggers

- aggregate sales data for forecasting
- extract tsfeatures from polars
- prepare panel data for time series analysis
- analyze seasonality with tsfeatures


---
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, Marketing
Roles: Data Scientist, Growth Marketer, Marketing Analyst
Specialties: API, Database
Tools: Polars, Statsforecast, Tsfeatures
Country applicability: Country not specified
Classification basis: Source catalog labels
Instruction SHA-256: 8caf3cc9329d5cfc9f3d77308c223087421672fa7c6858df3d45361ba0532944
