From d0e5f6b5f1518a3b5ea9e3f3bfe2bbb49951aacc Mon Sep 17 00:00:00 2001 From: FBumann <117816358+FBumann@users.noreply.github.com> Date: Mon, 20 Apr 2026 09:37:26 +0200 Subject: [PATCH] fix: xarray 2026.4 rejects Dataset-into-Dataset construction MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit xarray 2026.4.0 raises TypeError when Dataset() is called with an existing Dataset as data_vars ("Use ds.copy() instead"). Three call sites in expressions.py wrapped an already-constructed Dataset returned by .transpose() / .assign_coords() — drop the redundant wrap. Model.parameters setter may also receive a Dataset; switch to .copy() in that case. Co-Authored-By: Claude Opus 4.7 (1M context) --- linopy/expressions.py | 8 ++++---- linopy/model.py | 4 +++- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/linopy/expressions.py b/linopy/expressions.py index ca491c3e..9153733a 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,8 @@ 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)) + # transpose to ensure correct dimension order + data = data.transpose(..., TERM_DIM) # ensure helper dimensions are not set as coordinates if drop_dims := set(HELPER_DIMS).intersection(data.coords): @@ -2098,7 +2098,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: