Skip to content

fix(variables): Variable.where works with solution attached#790

Merged
FabianHofmann merged 2 commits into
masterfrom
fix/variable-where-with-solution
Jun 24, 2026
Merged

fix(variables): Variable.where works with solution attached#790
FabianHofmann merged 2 commits into
masterfrom
fix/variable-where-with-solution

Conversation

@FabianHofmann

Copy link
Copy Markdown
Collaborator

Note

This PR's description and code changes were generated with AI assistance (Claude Code) and reviewed by a human.

Changes proposed in this Pull Request

Variable.where raised ValueError: exact match required for all data variable names as soon as a solution was attached to the model (after Model.solve) or the variable was fixed.

Cause: Variable.where passes a fill dict covering only labels/lower/upper to Dataset.where, which requires the fill keys to exactly match the dataset's data variables. After solving (or fix()), the variable's data gains extra columns (solution, _stashed_lower, _stashed_upper), so the match fails.

Fix: Augment the fill dict to cover every data variable present, defaulting extras to NaN. The explicit labels=-1 / lower=upper=NaN entries still override; only auxiliary columns pick up the NaN default.

This routes through Variable.fillna too, since it delegates to where. Audited the related methods — ffill/bfill (Dataset.where(cond) + Dataset.fillna(dict)) and shift (Dataset.shift(fill_value=dict)) already tolerate extra data variables, and LinearExpression/QuadraticExpression/Constraint are unaffected (they don't store solution/dual).

Added a regression test test_variable_where_with_solution and a release note.

Checklist

  • AI-generated content is marked (see AGENTS.md).
  • Code changes are sufficiently documented; i.e. new functions contain docstrings and further explanations may be given in doc.
  • Unit tests for new features were added (if applicable).
  • A note for the release notes doc/release_notes.rst of the upcoming release is included.
  • I consent to the release of this PR's code under the MIT license.

Variable.where passed a fill dict covering only labels/lower/upper to
Dataset.where, which requires an exact match against the dataset's data
variables. After solve() or fix() the variable carries extra columns
(solution, stashed bounds), so where() raised a ValueError. Augment the
fill dict to cover all present data variables, defaulting extras to NaN.
@codspeed-hq

codspeed-hq Bot commented Jun 24, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 138 untouched benchmarks
⏩ 138 skipped benchmarks1


Comparing fix/variable-where-with-solution (e3c50c7) with master (d810de7)

Open in CodSpeed

Footnotes

  1. 138 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports.

@FabianHofmann FabianHofmann merged commit 2d47d4a into master Jun 24, 2026
23 of 24 checks passed
@FabianHofmann FabianHofmann deleted the fix/variable-where-with-solution branch June 24, 2026 12:05
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.

1 participant