Repository navigation
Migrate WindowFeatures to narwhals, add polars support - #1077
Merged
Merged
Conversation
solegalli
force-pushed
the
narwhals-window-features
branch
from
November 21, 2026 09:27
f6b794b to
b793d2b
Compare
solegalli
changed the base branch from
narwhals-migration
to
narwhals-lag-features
November 21, 2026 09:27
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
force-pushed
the
narwhals-window-features
branch
from
November 25, 2026 18:58
b793d2b to
c354c6c
Compare
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>
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.
WindowFeaturesnow takes pandas and polars dataframes.What changes
"3D",freq,min_periodsand every pandas rolling function. It calls the rolling methods (rolling.mean(), ...) instead ofrolling.agg(), which is faster with many variables and gives the same output.periodsrows:mean,sum,std,var,countandsemuse narwhals' rolling expressions;min,max,median,skew,kurtandrankuse polars' own, because narwhals has none;first,lastandnuniqueaggregate the rows of each window, because polars has no rolling function for them.fit: time-span windows, other functions andmin_periods=0raiseNotImplementedError, asfreqdoes.add_window_featuresandcheck_window_parameters, intimeseries/forecasting/_forecasting_helpers.py.first,lastandrank.Behaviour changes
__init__now checkswindowandmin_periods, and rejects:windowset to 0, a negative number,Trueor[];min_periods, or one that isn't an integer;functions=[]or"";periods=True.sort_indexthat isn'tTrueorFalse, which used to be ignored silently.Before, these failed later in pandas or polars. The error messages end with "Got ... instead.".
return_empty=Truewith no numerical variables and a list of windows used to raiseNo objects to concatenate. It now returns the dataframe unchanged.With pandas,
corrandcovstill run as onmain: 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
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, shuffled rows and missing data.datetime_subtraction.pyerror.fit+transform, in ms:mainwindow=[3, 7], mean and stdwindow=5, max and median,drop_nawindow=5, max and median,drop_nawindow=[3, 7], mean and stdNew polars functions, 2M × 5,
window=[3, 10], in ms:first108 andlast116 (pandas 100 and 97),sem93 (pandas 339),nunique223 (pandas 1,009),rank275 (pandas 2,301).Needs decision
min_samples=, which polars 1.21 introduced.pyproject.tomlasks forpolars>=1.0.0, so we would raise the minimum to 1.21.rankneeds polars 1.35.Open
skew0 andkurt-3, and polars gives NaN.first,lastandnuniquedon't exist, andsemdivides by √(n − 1). The tests skip them there.