Add Variable.reindex (mirroring LinearExpression.reindex)
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 257
- Forks
- 87
- Avg merge
- 1d 3h
- Merged PRs (30d)
- 29
Description
[!NOTE]
This issue was drafted by AI (Claude).
Describe the feature you'd like to see
LinearExpression has a .reindex method, but Variable does not — so a Variable cannot be aligned to a new coordinate set directly. To reindex a variable today you have to round-trip through .to_linexpr() (or reindex a derived DataArray such as .isnull() separately), which is awkward when you just want to align the variable itself.
import pandas as pd
from linopy import Model
m = Model()
names = pd.Index(["a", "b"], name="name")
v = m.add_variables(coords=[names], name="x")
hasattr(v, "reindex") # False
hasattr(v.to_linexpr(), "reindex") # True
v.reindex(name=pd.Index(["a", "b", "c"], name="name")) # AttributeError
A Variable.reindex(...) mirroring LinearExpression.reindex (and the underlying xarray semantics) would let callers align a variable to a target index in a single expression, without converting to a LinearExpression first.
Masking behavior
A masked entry already carries variable label -1, the same sentinel reindex uses for a freshly-introduced coordinate — so masking falls out with a single notion of "absent":
- New coordinates fill as masked/null, not with a value (
fill_valuedoesn't apply to variable labels — there's no variable to fill). - Existing masks are preserved; subsetting just drops entries.
isnull()stays mask-aware: it reportsTruefor both pre-existing masks and newly-missing coords, with no special-casing.
v.reindex(name=["a", "b", "c"]).isnull() # [False, True, True]
# pre-existing mask ^ ^ new coord
The value is a correct, mask-aware null array at the Variable level — before to_linexpr(), which is exactly where one wants to branch on availability. Constant fills (e.g. 1 for "always on") remain a LinearExpression concern (to_linexpr().reindex(...).where(...)); Variable.reindex stays pure: variables or masked, nothing else.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by locating LinearExpression.reindex and the Variable paths for to_linexpr and isnull. Trace how coordinate alignment and the -1 variable-label sentinel are represented, then implement the matching Variable entry point. Done means new coordinates are masked, existing masks are preserved, and isnull() reports both kinds of missing entries.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- data
- Issue type
- Feature
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 58/100