From 619b07c2b136069a29f730b52138008fbf0bc036 Mon Sep 17 00:00:00 2001 From: FBumann <117816358+FBumann@users.noreply.github.com> Date: Tue, 20 Jan 2026 20:26:53 +0100 Subject: [PATCH 1/2] Fix as_dataarray to apply coords parameter for DataArray input Previously, when a DataArray was passed to as_dataarray(), the coords parameter was silently ignored. This was inconsistent with other input types (numpy, pandas) where coords are applied. Now, when coords is provided as a dict and the input is a DataArray, the function will reindex the array to match the provided coordinates. This ensures consistent behavior across all input types. Co-Authored-By: Claude Opus 4.5 --- linopy/common.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/linopy/common.py b/linopy/common.py index 7dd97b65..8554a9c1 100644 --- a/linopy/common.py +++ b/linopy/common.py @@ -9,7 +9,7 @@ import operator import os -from collections.abc import Callable, Generator, Hashable, Iterable, Sequence +from collections.abc import Callable, Generator, Hashable, Iterable, Mapping, Sequence from functools import partial, reduce, wraps from pathlib import Path from typing import TYPE_CHECKING, Any, Generic, TypeVar, overload @@ -264,7 +264,13 @@ def as_dataarray( arr = DataArray(float(arr), coords=coords, dims=dims, **kwargs) elif isinstance(arr, int | float | str | bool | list): arr = DataArray(arr, coords=coords, dims=dims, **kwargs) - + elif isinstance(arr, DataArray): + # Apply coords via reindex if provided as dict (for consistency with other input types) + if coords is not None and isinstance(coords, Mapping): + # Only reindex dimensions that exist in both arr and coords + reindex_coords = {k: v for k, v in coords.items() if k in arr.dims} + if reindex_coords: + arr = arr.reindex(reindex_coords) elif not isinstance(arr, DataArray): supported_types = [ np.number, From 44a94b5c5335151f7004cafe9914645f5d585c45 Mon Sep 17 00:00:00 2001 From: FBumann <117816358+FBumann@users.noreply.github.com> Date: Tue, 20 Jan 2026 20:46:11 +0100 Subject: [PATCH 2/2] 1. Reindexes existing dims to match coord order 2. Expands to new dims from coords (broadcast) Summary: - as_dataarray now consistently applies coords for all input types - DataArrays with fewer dims are expanded to match the full coords specification - This fixes the inconsistency when creating variables with DataArray bounds --- linopy/common.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/linopy/common.py b/linopy/common.py index 8554a9c1..ab8ea6f2 100644 --- a/linopy/common.py +++ b/linopy/common.py @@ -265,12 +265,16 @@ def as_dataarray( elif isinstance(arr, int | float | str | bool | list): arr = DataArray(arr, coords=coords, dims=dims, **kwargs) elif isinstance(arr, DataArray): - # Apply coords via reindex if provided as dict (for consistency with other input types) + # Apply coords via reindex/expand if provided as dict (for consistency with other input types) if coords is not None and isinstance(coords, Mapping): - # Only reindex dimensions that exist in both arr and coords + # Reindex dimensions that exist in both arr and coords reindex_coords = {k: v for k, v in coords.items() if k in arr.dims} if reindex_coords: arr = arr.reindex(reindex_coords) + # Expand to new dimensions from coords + expand_coords = {k: v for k, v in coords.items() if k not in arr.dims} + if expand_coords: + arr = arr.expand_dims(expand_coords) elif not isinstance(arr, DataArray): supported_types = [ np.number,