Skip to content

Add Variable.reindex (mirroring LinearExpression.reindex) #800

Description

@FabianHofmann

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":

  1. New coordinates fill as masked/null, not with a value (fill_value doesn't apply to variable labels — there's no variable to fill).
  2. Existing masks are preserved; subsetting just drops entries.
  3. isnull() stays mask-aware: it reports True for 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions