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

Emit flat/to_polars directly from the CSR store

Open
#968 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
68/100
Issue type
Feature
Clarity
Clearly specified
Activity status
Active
Tech stack
python
Domain
data

Research direction

Start with LinearExpression.flat and to_polars in linopy/expressions.py, then read csr_to_term_arrays in linopy/csr.py and CSRConstraint.to_polars in linopy/constraints.py as the stated precedent. Trace Grid.indexer, strides, and indexes to assemble the coordinate columns. Done means CSR-backed expressions emit the compact canonical rows directly and flat shares that path without dense expansion.

Written by the indexing model from the issue text.

Description

enhancement performance sparse

[!NOTE]
The following content was generated by AI.

Describe the feature you'd like to see

Emit flat and to_polars directly from the CSR store, without building the dense rectangle first. Child of #756.

LinearExpression.flat (linopy/expressions.py:2612) and to_polars (2693) both read self.data, expand to the padded coord_dims x _term rectangle, convert to a long frame and then mask the padding straight back out. For a CSR-backed expression that is a round trip through exactly the representation the long format is trying to reach.

This matters twice: it is the export boundary, and it is the natural way to inspect a sparse expression, so it currently doubles as a densification trap.

Implementation ideas

csr_to_term_arrays (linopy/csr.py:506-532) already produces the flat term arrays. The work is to assemble the coordinate columns from Grid (indexer, strides, indexes) and emit the polars frame directly, then have flat share that path.

CSRConstraint.to_polars (linopy/constraints.py:1271) is the existing precedent on the constraint side and should be the model for the expression side.

Note that a CSR-backed result is compact canonical form, so the emitted row count legitimately differs from the dense path where explicit zeros were pruned. See #925.

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.