Skip to content

Commit 20b2831

Browse files
committed
Update Changelog and add some tests to ensure behaviour
1 parent 490acee commit 20b2831

2 files changed

Lines changed: 82 additions & 0 deletions

File tree

doc/release_notes.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ Release Notes
44
Upcoming Version
55
----------------
66

7+
* When passing DataArray bounds to ``add_variables`` with explicit ``coords``, the ``coords`` parameter now defines the variable's coordinates. DataArray bounds are validated against these coords (raises ``ValueError`` on mismatch) and broadcast to missing dimensions. Previously, the ``coords`` parameter was silently ignored for DataArray inputs.
8+
79
Version 0.6.4
810
--------------
911

test/test_common.py

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -432,6 +432,86 @@ def test_as_dataarray_with_dataarray_default_dims_coords() -> None:
432432
assert list(da_out.coords["dim2"].values) == list(da_in.coords["dim2"].values)
433433

434434

435+
def test_as_dataarray_with_dataarray_coord_mismatch() -> None:
436+
da_in = DataArray([1, 2, 3], dims=["x"], coords={"x": [10, 20, 30]})
437+
with pytest.raises(ValueError, match="do not match"):
438+
as_dataarray(da_in, coords={"x": [10, 20, 40]})
439+
440+
441+
def test_as_dataarray_with_dataarray_extra_dims() -> None:
442+
da_in = DataArray([[1, 2], [3, 4]], dims=["x", "y"])
443+
with pytest.raises(ValueError, match="extra dimensions"):
444+
as_dataarray(da_in, coords={"x": [0, 1]})
445+
446+
447+
def test_as_dataarray_with_dataarray_extra_dims_allowed() -> None:
448+
da_in = DataArray(
449+
[[1, 2], [3, 4]],
450+
dims=["x", "y"],
451+
coords={"x": [0, 1], "y": [0, 1]},
452+
)
453+
da_out = as_dataarray(da_in, coords={"x": [0, 1]}, allow_extra_dims=True)
454+
assert da_out.dims == da_in.dims
455+
assert da_out.shape == da_in.shape
456+
457+
458+
def test_as_dataarray_with_dataarray_broadcast() -> None:
459+
da_in = DataArray([1, 2], dims=["x"], coords={"x": ["a", "b"]})
460+
da_out = as_dataarray(
461+
da_in, coords={"x": ["a", "b"], "y": [1, 2, 3]}, dims=["x", "y"]
462+
)
463+
assert set(da_out.dims) == {"x", "y"}
464+
assert da_out.sizes["y"] == 3
465+
466+
467+
def test_as_dataarray_with_dataarray_no_coords() -> None:
468+
da_in = DataArray([1, 2, 3], dims=["x"], coords={"x": [10, 20, 30]})
469+
da_out = as_dataarray(da_in)
470+
assert_equal(da_out, da_in)
471+
472+
473+
def test_as_dataarray_with_dataarray_sequence_coords() -> None:
474+
da_in = DataArray([1, 2], dims=["x"], coords={"x": [0, 1]})
475+
idx = pd.RangeIndex(2, name="x")
476+
da_out = as_dataarray(da_in, coords=[idx], dims=["x"])
477+
assert list(da_out.coords["x"].values) == [0, 1]
478+
479+
480+
def test_as_dataarray_with_dataarray_sequence_coords_mismatch() -> None:
481+
da_in = DataArray([1, 2], dims=["x"], coords={"x": [0, 1]})
482+
idx = pd.RangeIndex(3, name="x")
483+
with pytest.raises(ValueError, match="do not match"):
484+
as_dataarray(da_in, coords=[idx], dims=["x"])
485+
486+
487+
def test_add_variables_with_dataarray_bounds_and_coords() -> None:
488+
model = Model()
489+
time = pd.RangeIndex(5, name="time")
490+
lower = DataArray([0, 0, 0, 0, 0], dims=["time"], coords={"time": range(5)})
491+
var = model.add_variables(lower=lower, coords=[time], name="x")
492+
assert var.shape == (5,)
493+
assert list(var.data.coords["time"].values) == list(range(5))
494+
495+
496+
def test_add_variables_with_dataarray_bounds_coord_mismatch() -> None:
497+
model = Model()
498+
time = pd.RangeIndex(5, name="time")
499+
lower = DataArray([0, 0, 0], dims=["time"], coords={"time": [0, 1, 2]})
500+
with pytest.raises(ValueError, match="do not match"):
501+
model.add_variables(lower=lower, coords=[time], name="x")
502+
503+
504+
def test_add_variables_with_dataarray_bounds_broadcast() -> None:
505+
model = Model()
506+
time = pd.RangeIndex(3, name="time")
507+
space = pd.Index(["a", "b"], name="space")
508+
lower = DataArray([0, 0, 0], dims=["time"], coords={"time": range(3)})
509+
var = model.add_variables(lower=lower, coords=[time, space], name="x")
510+
assert set(var.data.dims) == {"time", "space"}
511+
assert var.data.sizes["time"] == 3
512+
assert var.data.sizes["space"] == 2
513+
514+
435515
def test_as_dataarray_with_unsupported_type() -> None:
436516
with pytest.raises(TypeError):
437517
as_dataarray(lambda x: 1, dims=["dim1"], coords=[["a"]])

0 commit comments

Comments
 (0)