Skip to content

Migrate ExpandingWindowFeatures to narwhals, add polars support - #1071

Open
solegalli wants to merge 3 commits into
narwhals-migrationfrom
narwhals-expanding-window-features
Open

solegalli wants to merge 3 commits into
narwhals-migrationfrom
narwhals-expanding-window-features

Conversation

@solegalli

@solegalli solegalli commented Sep 19, 2026 •

Copy link
Copy Markdown
Collaborator

ExpandingWindowFeatures now takes pandas and polars dataframes.

What changes

  • pandas works as before: expanding(min_periods).agg(functions).shift(periods, freq), merged on the index.
  • polars uses the rows in the order given, shifted by periods rows:
    • it takes the 15 pandas expanding functions that aggregate one variable, with the same results as pandas 3;
    • sum, mean, std, var and sem use narwhals' rolling expressions, with a window as long as the dataframe;
    • count, min, max, first, last and nunique use cumulative narwhals expressions;
    • median, skew, kurt and rank use polars' own rolling functions, because narwhals has none;
    • the results are Float64, like pandas. With missing_values="ignore", NaN and inf are missing data, as in pandas.
  • polars, in fit: other functions raise NotImplementedError, as freq does. The check is shared with WindowFeatures.
  • The code that depends on the library lives in add_expanding_window_features, 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.

Behaviour changes

  • __init__ now rejects:

    • a negative min_periods, or one that isn't an integer;
    • functions=[] or "";
    • periods=True;
    • a sort_index that isn't True or False.

    The error messages end with "Got ... instead.". The functions message had a missing space, and the duplicated functions message changes.

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

  • User guide: fixed a missing bracket and period=1 (now periods), and moved "Getting the name of the new features" before the polars section, which redefined X.

Checks

  • Outputs: identical to narwhals-migration in 178 pandas cases, on pandas 3 and 2.3. Identical to the previous version of this PR in 266 cases on pandas and polars, except the 16 polars cases with the new functions, which used to raise. This covers transform, transform_x_y, get_feature_names_out, reordered columns, shuffled rows and missing data.
  • polars against pandas: the same values for the 15 functions with min_periods None, 0, 1, 2, 5 and 100, periods 0, 1 and 3, NaN and inf.
  • Docs: the user guide outputs and both docstring examples match what the code prints.
  • Tests: the full suite has only the 8 failures already on narwhals-migration. flake8 is clean, and mypy shows only the known datetime_subtraction.py error. main's tests pass, except 3 that compare the old error messages.
  • Speed, whole fit + transform, 2M × 5, in ms:
functions pandas polars
mean 55 34
sum, mean, min, max, std 391 78
median 13,242 411
first / last 52 / 50 31 / 21
nunique 2,021 142
sem 130 51
skew / kurt 72 / 72 139 / 118
rank 13,764 7,427

The functions polars already had run as fast as in the previous version of this PR.

Needs decision

  • Minimum polars version. As in WindowFeatures: min_samples= needs polars 1.21 and rank needs 1.35, while pyproject.toml asks for polars>=1.0.0.

Open

  • When all the values so far are equal, pandas gives skew 0 and kurt -3, and polars gives NaN, as in WindowFeatures.
  • With polars, skew and kurt are about 2x slower than pandas, which updates running moments instead of using a long rolling window.
  • With pandas 2.3, first, last and nunique don't exist, and sem divides by √(n − 1). The tests skip them there.
  • median, skew, kurt and rank call polars functions, so other libraries that narwhals supports, like modin, would fail on them in transform.
  • transform_x_y with the target as a list or an array waits for the fix in fix-transform-xy-targets.

@solegalli
solegalli force-pushed the narwhals-expanding-window-features branch from e20de4c to 8c2e063 Compare November 21, 2026 09:27
@solegalli
solegalli changed the base branch from narwhals-migration to narwhals-window-features November 21, 2026 09:27
@solegalli
solegalli force-pushed the narwhals-window-features branch from b793d2b to c354c6c Compare November 25, 2026 18:58
Base automatically changed from narwhals-window-features to narwhals-migration November 26, 2026 15:02
solegalli and others added 3 commits November 26, 2026 16:03
pandas keeps its expanding().agg().shift() path. With polars, the
features are built with narwhals rolling and cumulative expressions, and
the polars rolling median, and give the same values as pandas.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
With polars, ExpandingWindowFeatures now takes the 15 pandas expanding
functions that aggregate one variable: first, last, nunique and sem with
narwhals, and skew, kurt and rank with polars, as median. __init__ validates
min_periods and sort_index, rejects periods=True, and its messages end with
"Got ... instead.". The helpers have no early returns, and the functions check
is shared with WindowFeatures.

The tests check the column order and drop_original with drop_na on both
libraries, and the user guide shows the feature names before the polars
section, which redefined X.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@solegalli
solegalli force-pushed the narwhals-expanding-window-features branch from 8c2e063 to e429e28 Compare November 26, 2026 15:17
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