Skip to content

Migrate WindowFeatures to narwhals, add polars support - #1077

Merged
solegalli merged 7 commits into
narwhals-migrationfrom
narwhals-window-features
Nov 26, 2026
Merged

solegalli merged 7 commits into
narwhals-migrationfrom
narwhals-window-features

Conversation

@solegalli

@solegalli solegalli commented Sep 19, 2026 •

Copy link
Copy Markdown
Collaborator

WindowFeatures now takes pandas and polars dataframes.

What changes

  • pandas works as before: time-span windows like "3D", freq, min_periods and every pandas rolling function. It calls the rolling methods (rolling.mean(), ...) instead of rolling.agg(), which is faster with many variables and gives the same output.
  • polars uses the rows in the order given, with windows of rows shifted by periods rows:
    • it takes the 15 pandas rolling functions that aggregate one variable, with the same results as pandas 3;
    • mean, sum, std, var, count and sem use narwhals' rolling expressions;
    • min, max, median, skew, kurt and rank use polars' own, because narwhals has none;
    • first, last and nunique aggregate the rows of each window, because polars has no rolling function for them.
  • polars, in fit: time-span windows, other functions and min_periods=0 raise NotImplementedError, as freq does.
  • The code that depends on the library lives in add_window_features and check_window_parameters, in timeseries/forecasting/_forecasting_helpers.py.
  • The docstring and the user guide list the polars functions and link to the pandas ones. The user guide has polars examples, including first, last and rank.

Behaviour changes

  • __init__ now checks window and min_periods, and rejects:

    • window set to 0, a negative number, True or [];
    • a negative min_periods, or one that isn't an integer;
    • functions=[] or "";
    • periods=True.
    • a sort_index that isn't True or False, which used to be ignored silently.

    Before, these failed later in pandas or polars. The error messages end with "Got ... instead.".

  • return_empty=True with no numerical variables and a list of windows used to raise No objects to concatenate. It now returns the dataframe unchanged.

  • With pandas, corr and cov still run as on main: they aren't single-variable aggregations, so they aren't documented.

  • polars keeps integer results as integers (sum, min, max). pandas returns floats, as before.

Checks

  • Outputs: identical to narwhals-migration on pandas 3 and 2.3, and to the previous version of this PR on pandas and polars. This covers transform, transform_x_y, get_feature_names_out, reordered columns, shuffled rows and missing data.
  • Docs: the user guide outputs and both docstring examples match what the code prints.
  • Tests: the time series tests pass. flake8 is clean, and mypy shows only the known datetime_subtraction.py error.
  • Speed, whole fit + transform, in ms:
case main this PR
pandas 10k × 10, window=[3, 7], mean and std 11.4 5.3
pandas 500k × 10, window=5, max and median, drop_na 716 691
pandas 10k × 1,000, window=5, max and median, drop_na 2,022 1,438
polars 2M × 10, window=[3, 7], mean and std – 183

New polars functions, 2M × 5, window=[3, 10], in ms: first 108 and last 116 (pandas 100 and 97), sem 93 (pandas 339), nunique 223 (pandas 1,009), rank 275 (pandas 2,301).

Needs decision

  1. Minimum polars version. The polars functions use min_samples=, which polars 1.21 introduced. pyproject.toml asks for polars>=1.0.0, so we would raise the minimum to 1.21. rank needs polars 1.35.
  2. narwhals plus polars expressions, or only polars expressions. With mixed functions and few variables, only polars expressions was up to 1.5x faster. It was the same with 10 variables.

Open

  • When all the values of a window are equal, pandas gives skew 0 and kurt -3, and polars gives NaN.
  • With pandas 2.3, first, last and nunique don't exist, and sem divides by √(n − 1). The tests skip them there.

@solegalli
solegalli force-pushed the narwhals-window-features branch from f6b794b to b793d2b Compare November 21, 2026 09:27
@solegalli
solegalli changed the base branch from narwhals-migration to narwhals-lag-features November 21, 2026 09:27
Base automatically changed from narwhals-lag-features to narwhals-migration November 25, 2026 18:54
solegalli and others added 4 commits November 25, 2026 19:55
WindowFeatures takes pandas and polars dataframes. With pandas it keeps
rolling on the index, including time-span windows, freq and all rolling
functions; it now calls the rolling methods instead of agg(), which is
faster with many variables. With polars the rows are used in the order
given, windows are numbers of rows, and mean, sum, std, var and count use
narwhals, while min, max, median, skew and kurt use polars expressions.
Time-span windows, other functions and min_periods=0 raise a clear error
with polars.

Also stops return_empty=True from failing with lists of windows or
functions when there are no numerical variables.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…lper

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@solegalli
solegalli force-pushed the narwhals-window-features branch from b793d2b to c354c6c Compare November 25, 2026 18:58
solegalli and others added 3 commits November 26, 2026 14:55
With polars, WindowFeatures now takes the 15 pandas rolling functions that
aggregate one variable. sem uses narwhals, rank uses polars' rolling_rank,
and first, last and nunique aggregate the rows of each window. The docstring
and the user guide list the polars functions and link to the pandas ones.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
check_window_parameters runs its checks in an if block instead of returning
early, and transform and _get_new_features_name turn window and functions into
lists inline instead of calling _functions_list.

Adds the cases main tested that the migrated tests missed: several windows with
periods and freq, drop_original with drop_na, the column order with several
windows and functions, and input_features as the pandas columns.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…nventions

WindowFeatures now rejects a sort_index that isn't a boolean, as LagFeatures
does. The tests check the column order of the window helper, vary every
init parameter, check the type of every output and parametrize the duplicated
functions error. The polars row-order test is removed, because the base tests
cover it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@solegalli
solegalli merged commit 4cb90ff into narwhals-migration Nov 26, 2026
4 of 10 checks passed
@solegalli
solegalli deleted the narwhals-window-features branch November 26, 2026 15:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant