Fix/monotonic drop first - #1002
Merged
Matthias Schmidtblaicher (MatthiasSchmidtblaicherQC) merged 7 commits intoMay 5, 2026
Merged
Fix/monotonic drop first#1002Matthias Schmidtblaicher (MatthiasSchmidtblaicherQC) merged 7 commits into
Matthias Schmidtblaicher (MatthiasSchmidtblaicherQC) merged 7 commits into
Conversation
Formulaic appends a '-' (or '+') suffix to factor names in the model spec
when drop_first=True to signal rank reduction. The constraint resolution
code passed this suffixed name (e.g. "C(age)-") to _build_monotonic_constraints,
which failed to find it in feature_names_ (which use "C(age)[1]" etc.).
Strip the suffix with rstrip("-+") before the lookup.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Models pickled before this attribute existed will get None as default when unpickled, avoiding AttributeError. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Matthias Schmidtblaicher (MatthiasSchmidtblaicherQC)
requested a review
from Copilot
May 4, 2026 15:48
Contributor
There was a problem hiding this comment.
Pull request overview
This PR fixes a ValueError encountered when using monotonic_constraints with categorical factors under drop_first=True by normalizing Formulaic factor names during constraint resolution, and it restores pickle backward compatibility for models saved before the monotonic_constraints attribute existed.
Changes:
- Strip Formulaic’s trailing
-/+suffixes from factor names before matching them to generated feature names for monotonic constraint construction. - Add a missing default for
monotonic_constraintsin__setstate__to support unpickling older models. - Add a regression test covering
C(...)withdrop_first=Trueand monotonic constraints; document the fix in the changelog.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| tests/glm/test_formula.py | Adds regression test for categorical C(...) with drop_first=True under monotonic_constraints. |
| src/glum/_glm.py | Ensures older pickles load by defaulting missing monotonic_constraints in __setstate__. |
| src/glum/_formula.py | Normalizes factor names via suffix stripping before monotonic constraint lookup. |
| CHANGELOG.rst | Adds 3.4.1 unreleased notes describing the bug fix and pickle compatibility. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Matthias Schmidtblaicher (MatthiasSchmidtblaicherQC)
marked this pull request as ready for review
May 4, 2026 15:58
Jan Tilly (jtilly)
approved these changes
May 4, 2026
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Matthias Schmidtblaicher (MatthiasSchmidtblaicherQC)
merged commit May 5, 2026
a25bdcf
into
main
25 checks passed
Matthias Schmidtblaicher (MatthiasSchmidtblaicherQC)
deleted the
fix/monotonic-drop-first
branch
May 5, 2026 07:13
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.
Summary
Summary
ValueError: Factor 'C(x)-' not found in feature_nameswhen usingmonotonic_constraintswithdrop_first=Trueon categorical factors.monotonic_constraints.Root cause
_resolve_monotonic_constraints_from_model_specreads factor names viastr(ScopedFactor), which appends a-suffix when the factor is rank-reduced (drop_first=True). Feature names never carry this suffix (e.g."C(education)[1.0]"not"C(education)-[1.0]"), so the downstream lookup in_build_monotonic_constraintsfails.Fix: strip trailing
-/+from the factor name before passing it on.Test plan
test_monotonic_constraints_categorical_drop_firstChecklist
CHANGELOG.rstentry