From 9e6c27d35f9227d07dcafb2a3c3b310873183655 Mon Sep 17 00:00:00 2001 From: Florian Maurer Date: Wed, 15 Apr 2026 12:04:35 +0200 Subject: [PATCH 1/2] fix: use xarray.Dataset copy instead of constructor since the latest xarray version, passing a Dataset as `data_vars` to the Dataset constructor is not supported. transpose and assign_coords already returns a new dataset. --- doc/release_notes.rst | 1 + linopy/expressions.py | 7 +++---- linopy/model.py | 4 +++- 3 files changed, 7 insertions(+), 5 deletions(-) diff --git a/doc/release_notes.rst b/doc/release_notes.rst index 54c98f43..4bcf0bf5 100644 --- a/doc/release_notes.rst +++ b/doc/release_notes.rst @@ -27,6 +27,7 @@ Upcoming Version * Add ``format_labels()`` on ``Constraints``/``Variables`` and ``format_infeasibilities()`` on ``Model`` that return strings instead of printing to stdout, allowing usage with logging, storage, or custom output handling. Deprecate ``print_labels()`` and ``print_infeasibilities()``. * Add ``fix()``, ``unfix()``, and ``fixed`` to ``Variable`` and ``Variables`` for fixing variables to values via equality constraints. Supports automatic rounding for integer/binary variables. * Add ``relax()``, ``unrelax()``, and ``relaxed`` to ``Variable`` and ``Variables`` for LP relaxation of integer/binary variables. Supports partial relaxation via filtered views (e.g. ``m.variables.integers.relax()``). Semi-continuous variables raise ``NotImplementedError``. +* Add compatibility to latest xarray version. Version 0.6.6 diff --git a/linopy/expressions.py b/linopy/expressions.py index ca491c3e..62620885 100644 --- a/linopy/expressions.py +++ b/linopy/expressions.py @@ -284,7 +284,7 @@ def sum(self, use_fallback: bool = False, **kwargs: Any) -> LinearExpression: index.names = [str(col) for col in orig_group.columns] index.name = GROUP_DIM new_coords = Coordinates.from_pandas_multiindex(index, GROUP_DIM) - ds = xr.Dataset(ds.assign_coords(new_coords)) + ds = ds.assign_coords(new_coords) ds = ds.rename({GROUP_DIM: final_group_name}) return LinearExpression(ds, self.model) @@ -391,8 +391,7 @@ def __init__(self, data: Dataset | Any | None, model: Model) -> None: coeffs_vars_dict = {str(k): v for k, v in coeffs_vars.items()} data = assign_multiindex_safe(data, **coeffs_vars_dict) - # transpose with new Dataset to really ensure correct order - data = Dataset(data.transpose(..., TERM_DIM)) + data = data.transpose(..., TERM_DIM) # ensure helper dimensions are not set as coordinates if drop_dims := set(HELPER_DIMS).intersection(data.coords): @@ -2098,7 +2097,7 @@ def __init__(self, data: Dataset | None, model: Model) -> None: raise ValueError(f"Size of dimension {FACTOR_DIM} must be 2.") # transpose data to have _term as last dimension and _factor as second last - data = xr.Dataset(data.transpose(..., FACTOR_DIM, TERM_DIM)) + data = data.transpose(..., FACTOR_DIM, TERM_DIM) self._data = data @property diff --git a/linopy/model.py b/linopy/model.py index 2a635680..4748db6f 100644 --- a/linopy/model.py +++ b/linopy/model.py @@ -359,7 +359,9 @@ def parameters(self, value: Dataset | Mapping) -> None: """ Set the parameters of the model. """ - self._parameters = Dataset(value) + self._parameters = ( + value.copy() if isinstance(value, Dataset) else Dataset(value) + ) @property def solution(self) -> Dataset: From 63f1df60caa6115979cf3c14c037232db0ad40cb Mon Sep 17 00:00:00 2001 From: Jonas Hoersch Date: Fri, 17 Apr 2026 15:21:57 +0200 Subject: [PATCH 2/2] Revert "fix: temporarily constrain xarray" This reverts commit 545b5636e12b992f61cd9f2f3ff605065c9cc596. --- pyproject.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index 87e79ab2..f012ebc6 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -31,7 +31,7 @@ dependencies = [ "bottleneck", "toolz", "numexpr", - "xarray>=2024.2.0,<2026.4", + "xarray>=2024.2.0", "dask>=0.18.0", "polars>=1.31.1", "tqdm",