Skip to content

Commit 42a036c

Browse files
committed
Improve performance
1 parent 5f2e94d commit 42a036c

1 file changed

Lines changed: 13 additions & 10 deletions

File tree

linopy/common.py

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -169,17 +169,19 @@ def _validate_dataarray_coords(
169169
if extra:
170170
raise ValueError(f"DataArray has extra dimensions not in coords: {extra}")
171171

172+
expand = {}
172173
for k, v in expected.items():
173-
if k in arr.dims:
174-
expected_idx = pd.Index(v)
175-
actual_idx = pd.Index(arr.coords[k].values)
176-
if not actual_idx.equals(expected_idx):
177-
raise ValueError(
178-
f"Coordinates for dimension '{k}' do not match: "
179-
f"expected {expected_idx.tolist()}, got {actual_idx.tolist()}"
180-
)
174+
if k not in arr.dims:
175+
expand[k] = v
176+
continue
177+
actual = arr.coords[k]
178+
v_idx = v if isinstance(v, pd.Index) else pd.Index(v)
179+
if not actual.to_index().equals(v_idx):
180+
raise ValueError(
181+
f"Coordinates for dimension '{k}' do not match: "
182+
f"expected {v_idx.tolist()}, got {actual.values.tolist()}"
183+
)
181184

182-
expand = {k: v for k, v in expected.items() if k not in arr.dims}
183185
if expand:
184186
arr = arr.expand_dims(expand)
185187

@@ -337,7 +339,8 @@ def as_dataarray(
337339
elif isinstance(arr, int | float | str | bool | list):
338340
arr = DataArray(arr, coords=coords, dims=dims, **kwargs)
339341
elif isinstance(arr, DataArray):
340-
arr = _validate_dataarray_coords(arr, coords, dims, allow_extra_dims)
342+
if coords is not None:
343+
arr = _validate_dataarray_coords(arr, coords, dims, allow_extra_dims)
341344
elif not isinstance(arr, DataArray):
342345
supported_types = [
343346
np.number,

0 commit comments

Comments
 (0)