Hacktoberfest 2026: the issues maintainers tagged for October, open and beginner-friendly. Browse Hacktoberfest issues

Add Variable.reindex (mirroring LinearExpression.reindex)

Open
#800 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
58/100
Issue type
Feature
Clarity
Mostly clear
Activity status
Quiet
Tech stack
python
Domain
data

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.

Description

data-model enhancement

[!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.

Dominant language
Python
Stars
257
Forks
87
Avg merge
1d 15h
Merged PRs (30d)
32

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from PyPSA/linopy

All issues in PyPSA/linopy

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.