Repository navigation
Migrate LagFeatures to narwhals, add polars support - #1073
Merged
Merged
Conversation
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
force-pushed
the
narwhals-lag-features
branch
from
November 21, 2026 09:27
222ef4f to
1e5aef8
Compare
This was referenced Nov 21, 2026
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…in freq Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
LagFeaturesnow takes pandas and polars dataframes.What changes
sort_indexsorts by it, andfreqshifts the DatetimeIndex.freqtakes strings and pandas offsets.freqraisesNotImplementedErrorinfit, andsort_indexhas no effect.add_lag_features, intimeseries/forecasting/_forecasting_helpers.py:shift+join;fill_value: polars' ownshift(fill_value=), which is 10x to 100x faster than narwhals;rows_with_missing_valuesmoves from DropMissingData todataframe_helpers.py. DropMissingData anddrop_nahere share it.Behaviour changes
__init__now rejectsperiods=True,[]and[True], andfreq=[]. Before, they failed later in pandas or polars, or gave odd columns (a_lag_True). The error messages end with "Got ... instead.".columns overlap. Onmainit added_x/_ysuffixes.Checks
narwhals-migrationon pandas 3 and 2.3, and to the previous version of this PR on pandas and polars. This coverstransform,transform_x_y,get_feature_names_out, reordered columns and the input left unchanged.narwhals-migration, in CI too. flake8 is clean, and mypy shows only the knowndatetime_subtraction.pyerror.fit+transform, in ms:mainperiods=[1, 2, 3]freq="1h"periods=[1, 2, 3]periods=[1, 2, 3]Open
transform_x_yfails with a list or numpy target, in the shared mixin. It goes in a separate PR.freq,fill_valuealso fills the missing values that come from the data, as onmain.