Skip to content

fix: DatetimeFeaturesAdder one-hot columns unstable and inconsistent with features_added()#980

Merged
egordm merged 6 commits into
OpenSTEF:mainfrom
Valyrian-Code:fix/datetime-features-onehot-columns
Jun 25, 2026
Merged

fix: DatetimeFeaturesAdder one-hot columns unstable and inconsistent with features_added()#980
egordm merged 6 commits into
OpenSTEF:mainfrom
Valyrian-Code:fix/datetime-features-onehot-columns

Conversation

@Valyrian-Code

Copy link
Copy Markdown
Contributor

What

With onehot_encode=True, DatetimeFeaturesAdder had two related problems:

  1. transform() built the month/quarter dummies with pd.get_dummies(data.index.month, ...), which only emits columns for the values present in the data. So the column set was data-dependent: training spanning Jan-Mar produced month_1..3, while inference in Apr produced month_4. Training and inference could therefore yield different feature columns.
  2. features_added() always returned the non-one-hot names (month_of_year, quarter_of_year), which the one-hot branch never produces, while omitting the actual month_N/quarter_N columns.

Fix

  • Build the dummies from a fixed category set (pd.Categorical(..., categories=1..12 / 1..4)) so the one-hot columns are always the full, stable set regardless of the data span.
  • Branch features_added() on onehot_encode so it reports exactly the columns transform() adds.

Tests

Added regression tests asserting set(features_added()) == set(added columns) for both branches, and that the one-hot columns are the full stable set even when the data spans only two months (the one-hot test fails on the previous code, passes with the fix). The existing one-hot test still passes.

ty check, ruff check, ruff format --check, the module doctest, and the full openstef-models suite all pass.

Note

This makes one-hot output always span all 12 months / 4 quarters. I believe that's the desired behaviour (a stable feature space across train/inference), but flagging it in case you'd prefer to keep it data-dependent.

… features_added()

With onehot_encode=True, transform() built month/quarter dummies only for the
values present in the data, so the column set was data-dependent (training and
inference could differ). features_added() also returned the non-one-hot names
(month_of_year/quarter_of_year), which the one-hot branch never produces, while
omitting the actual month_N/quarter_N columns.

Build the dummies from a fixed category set (all 12 months, 4 quarters) so the
one-hot columns are stable, and branch features_added() on onehot_encode so it
reports exactly the columns transform() adds. Add regression tests for both
branches.

Signed-off-by: RAJVEER42 <irajveer.bishnoi2310@gmail.com>
@Valyrian-Code Valyrian-Code requested a review from a team June 19, 2026 17:46

@lschilders lschilders left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

The comments in the method and the docstring in the test are way too verbose. Only add such comments when absolutely necessary. Fix and logic look good.

Address @lschilders review on OpenSTEF#980.

Signed-off-by: RAJVEER42 <irajveer.bishnoi2310@gmail.com>
@Valyrian-Code

Copy link
Copy Markdown
Contributor Author

Done, trimmed the comment in the one-hot branch and the test docstring. Thanks for the review!

@Valyrian-Code

Copy link
Copy Markdown
Contributor Author

@lschilders, any suggestion on the changes?

@egordm egordm left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Looks great! Good fix. We indeed want the full category values not just the ones available in data.

Thanks for your contribution!

@egordm egordm merged commit 501b8d6 into OpenSTEF:main Jun 25, 2026
3 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