Skip to content

Migrate LagFeatures to narwhals, add polars support - #1073

Merged
solegalli merged 4 commits into
narwhals-migrationfrom
narwhals-lag-features
Nov 25, 2026
Merged

solegalli merged 4 commits into
narwhals-migrationfrom
narwhals-lag-features

Conversation

@solegalli

@solegalli solegalli commented Sep 19, 2026 •

Copy link
Copy Markdown
Collaborator

LagFeatures now takes pandas and polars dataframes.

What changes

  • pandas works as before: the index gives the time order, sort_index sorts by it, and freq shifts the DatetimeIndex. freq takes strings and pandas offsets.
  • polars lags the rows in the order given. freq raises NotImplementedError in fit, and sort_index has no effect.
  • The code that depends on the library lives in add_lag_features, in timeseries/forecasting/_forecasting_helpers.py:
    • pandas: shift + join;
    • polars with fill_value: polars' own shift(fill_value=), which is 10x to 100x faster than narwhals;
    • narwhals otherwise.
  • rows_with_missing_values moves from DropMissingData to dataframe_helpers.py. DropMissingData and drop_na here share it.
  • The user guide and the docstring have a polars example.

Behaviour changes

  • __init__ now rejects periods=True, [] and [True], and freq=[]. Before, they failed later in pandas or polars, or gave odd columns (a_lag_True). The error messages end with "Got ... instead.".
  • If a lag name already exists in X, pandas raises columns overlap. On main it added _x/_y suffixes.
  • polars keeps integer lags as integers with nulls. pandas turns them into floats with NaN, 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 and the input left unchanged.
  • Docs: all 22 outputs in the user guide, and both docstring examples, match what the code prints.
  • Tests: the full suite fails only on the tests that also fail on narwhals-migration, in CI too. flake8 is clean, and mypy shows only the known datetime_subtraction.py error.
  • Speed, whole fit + transform, in ms:
case main this PR
pandas 100k × 10, periods=[1, 2, 3] 2.3 2.0
pandas 500k × 10, freq="1h" 10.7 10.6
pandas 10k × 1,000, periods=[1, 2, 3] 18.9 19.7
polars 2M × 10, periods=[1, 2, 3] – 46.5

Open

  • transform_x_y fails with a list or numpy target, in the shared mixin. It goes in a separate PR.
  • With freq, fill_value also fills the missing values that come from the data, as on main.

solegalli and others added 2 commits November 21, 2026 10:11
LagFeatures takes pandas and polars dataframes. It implements the
_add_features() hook of BaseForecastTransformer and inherits transform()
and transform_x_y() from it.

With pandas, the output is identical: the lags are joined on the index,
freq shifts on the DatetimeIndex and fill_value works as before. Joining
one freq lag at a time is 1.5-3x faster with lists of freq. With polars,
the rows are lagged in the order given, and fill_value only fills the rows
that have no past values.

Also adds the missing space in the sort_index error message, and a polars
example to the docstring and user guide.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
solegalli and others added 2 commits November 25, 2026 19:28
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…in freq

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@solegalli
solegalli merged commit bf347e4 into narwhals-migration Nov 25, 2026
4 of 10 checks passed
@solegalli
solegalli deleted the narwhals-lag-features branch November 25, 2026 18:54
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