Skip to content

fix: preserve fitted order of future exogenous columns - #1270

Merged
José Morales (jmoralez) merged 2 commits into
Nixtla:mainfrom
antonsoo:fix/future-exog-column-order
Oct 12, 2026
Merged

José Morales (jmoralez) merged 2 commits into
Nixtla:mainfrom
antonsoo:fix/future-exog-column-order

Conversation

@antonsoo

Copy link
Copy Markdown
Contributor

Select future covariate columns in the stored training order before converting X_df into a grouped array. The existing shape and required-name checks stay in place.

Currently the required features are checked by name but passed to the model in the incoming dataframe order. Reordering two named columns silently exchanges their fitted coefficients:

import numpy as np
import pandas as pd
from sklearn.linear_model import LinearRegression
from statsforecast import StatsForecast
from statsforecast.models import SklearnModel
from statsforecast.utils import AirPassengersDF

df = AirPassengersDF.copy()
df["trend"] = (df.ds.dt.year - 1949) * 12 + df.ds.dt.month
df["season"] = np.sin(2 * np.pi * df.ds.dt.month / 12)
sf = StatsForecast(models=[SklearnModel(LinearRegression())], freq="ME")
sf.fit(df.iloc[:-12])
future = df.iloc[-12:].drop(columns="y")
expected = sf.predict(h=12, X_df=future)
actual = sf.predict(h=12, X_df=future[["unique_id", "ds", "season", "trend"]])
pd.testing.assert_frame_equal(actual, expected)

This fails on current main, with a maximum prediction difference of about 2,654 passengers, and passes with this change.

Validation:

  • Twelve before-failing regression cases cover forecast, fit_predict, and fit().predict(), pandas/Polars, and one/two workers.
  • Real AirPassengers and monthly CO2 data, with native ARIMA and scikit-learn regression, were checked against separately fitted single-series models. Across 72 dtype/backend/worker/horizon/API configurations, 1,824 incorrect reordered forecast values become zero; the maximum reference difference is 5.69e-14. All 72 correctly ordered controls retain identical result bytes.
  • Core and focused tests: 60 passed. Project pre-commit hooks pass.

The real-data checks establish covariate alignment, not improved forecast accuracy. Distributed Spark/Dask/Ray execution was not exercised.

@CLAassistant

CLAassistant commented Oct 10, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@CLAassistant

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 10, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-12T21:31:42.440991Z 34cf5e8 New commits
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

Comment thread tests/test_future_exog_order.py Outdated

@jmoralez José Morales (jmoralez) left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Can you please sign the CLA? Once that's done this'll be ready to be merged

@jmoralez
José Morales (jmoralez) enabled auto-merge (squash) October 12, 2026 21:51
@jmoralez
José Morales (jmoralez) merged commit a274340 into Nixtla:main Oct 12, 2026
60 of 62 checks passed
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.

3 participants