Hacktoberfest 2026:维护者为十月标记出来的 issue,仍然开放、适合新手。 浏览 Hacktoberfest issue

add_variables drops non-dimension coordinates since #1078

未关闭 适合新手
#1,084 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

还没有人认领这个 Issue。

评估

难度
2/5
预计耗时
1-3 小时
新手友好度
78/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
python
领域
backend

调研方向

从 linopy/model.py 中的 _variables_on 开始,追踪 _coords_to_dict 如何处理包含非维度坐标的 xr.Coordinates。使用提供的示例复现,并检查 #1024 的往返测试。当 x.coords 保留 period,且测试明确断言该坐标仍然存在时,即完成。

由索引模型根据 Issue 内容生成。

描述

[!NOTE]
Drafted with AI assistance. The repro was run on both commits below.

Version Checks
  • I have confirmed this bug exists on the latest release of Linopy.
  • I have confirmed this bug exists on the current master branch of Linopy (b1d6a2e9).
Issue Description

Since #1078, add_variables drops non-dimension coordinates of an xr.Coordinates. The new fast path _variables_on (linopy/model.py:1016) builds its grid with _coords_to_dict, which keeps only dimension coordinates. The coordinates are gone before any constraint or netcdf code runs. This undoes #1024, whose test still passes because both sides of the round trip lack the coordinate.

@FabianHofmann I can open a small fix PR: fall back to the old path in _variables_on when coords carries non-dimension coordinates, and add an assert "time" in x.coords to the #1024 test. OK?

Reproducible Example
import numpy as np, xarray as xr, linopy

coords = xr.Coordinates({"snapshot": np.arange(4), "period": ("snapshot", [2020, 2020, 2030, 2030])})
x = linopy.Model().add_variables(lower=0, coords=coords, name="x")
print(list(x.coords))
# b1d6a2e9 (#1078): ['snapshot']
# 81891602 (before): ['snapshot', 'period']
Expected Behavior

x.coords keeps period, as before #1078.

Installed Versions

linopy master b1d6a2e9, Python 3.12.

主要语言
Python
星标
263
派生
92
平均合并
1 天 13 小时
30 天内合并 PR
32

环境准备

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

PyPSA/linopy 的其他 Issue

查看 PyPSA/linopy 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。