RollingStatisticsTransformer¶
yohou.preprocessing.window.RollingStatisticsTransformer
¶
Bases: BaseTransformer
Compute rolling window statistics for time series.
This transformer computes one or more rolling statistics (mean, std, min, max, median, quantiles) over sliding windows. It is a convenience wrapper around polars rolling functions with a sklearn-compatible interface.
Parameters¶
| Name | Type | Description | Default |
|---|---|---|---|
window_size
|
int
|
Size of the rolling window. Must be >= 1. |
7
|
statistics
|
str or list of str
|
Statistic(s) to compute. Options: - "mean": Rolling mean - "std": Rolling standard deviation - "min": Rolling minimum - "max": Rolling maximum - "median": Rolling median - "sum": Rolling sum - "var": Rolling variance - "q25": 25th percentile - "q75": 75th percentile |
"mean"
|
Attributes¶
| Name | Type | Description |
|---|---|---|
n_features_in_ |
int
|
Number of features seen during fit. |
feature_names_in_ |
list of str
|
Names of features seen during fit. |
statistics_ |
list of str
|
Effective list of statistics to compute. |
Examples¶
>>> import polars as pl
>>> from datetime import datetime
>>> from yohou.preprocessing import RollingStatisticsTransformer
>>> times = pl.datetime_range(
... start=datetime(2020, 1, 1), end=datetime(2020, 1, 10), interval="1d", eager=True
... )
>>> X = pl.DataFrame({"time": times, "value": [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0]})
>>> # Compute rolling mean with window size 3
>>> transformer = RollingStatisticsTransformer(window_size=3, statistics="mean")
>>> transformer.fit(X)
RollingStatisticsTransformer(window_size=3)
>>> X_t = transformer.transform(X)
>>> len(X_t)
8
>>> "value_mean" in X_t.columns
True
>>> # Multiple statistics
>>> transformer = RollingStatisticsTransformer(window_size=3, statistics=["mean", "std", "min", "max"])
>>> transformer.fit(X)
RollingStatisticsTransformer(...)
>>> X_t = transformer.transform(X)
>>> len([c for c in X_t.columns if c != "time"])
4
See Also¶
SlidingWindowFunctionTransformer : Apply custom function over windows.
LagTransformer : Create lagged features.
Notes¶
Rolling statistics are computed via native polars rolling expressions
(rolling_mean, rolling_std, etc.), which are significantly faster
than Python-level iteration. Quantile statistics (q25, q75) use
rolling_quantile with linear interpolation.
The first window_size - 1 rows produce nulls from incomplete windows
and are dropped from the output, setting
observation_horizon = window_size - 1.
Output column names follow the pattern {input_col}_{statistic},
e.g., "value_mean", "value_std".
Source Code¶
Show/Hide source
485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 | |
Methods¶
observation_horizon
property
¶
Return the number of past observations needed.
get_feature_names_out(input_features=None)
¶
Get output feature names for transformation.
Parameters¶
| Name | Type | Description | Default |
|---|---|---|---|
input_features
|
list of str or None
|
Column names of the input features. If |
None
|
Returns¶
| Type | Description |
|---|---|
list of str
|
Output feature names after transformation. |
Source Code¶
Show/Hide source
Tutorials¶
The following example notebooks use this component:
-
How to Compose Features with FeatureUnion
Data-Features
Combine lag features, rolling statistics, EMA, and scaling in parallel with FeatureUnion and automatic observation horizon resolution.
-
How to Build a Feature Pipeline
Data-Features
Nest FeaturePipeline, FeatureUnion, and DecompositionPipeline for multi-level feature engineering with trend-season-residual decomposition.
-
How to Apply Window Transformations
Data-Features
Feature engineering with LagTransformer, RollingStatisticsTransformer, SlidingWindowFunctionTransformer, and ExponentialMovingAverage on time series data.
-
How to Build Panel Feature Pipelines
Panel-Data
Combine ColumnForecaster, FeaturePipeline, FeatureUnion, and DecompositionPipeline on panel data with per-group scoring on KDD Cup air quality.
-
How to Preprocess Panel Data
Panel-Data
Automatic panel-aware transformation (StandardScaler, rolling stats, imputation) plus manual per-group workflows with get_group_df and dict_to_panel.