Skip to content

Commit f864718

Browse files
griveratshoyer
authored andcommitted
Raise proper error for scalar array when coords is a dict (#3271)
* Dims and shape check when coords is a dict * Update test to the correct raise message * Move logic and change raise message
1 parent aaeea62 commit f864718

File tree

2 files changed

+6
-1
lines changed

2 files changed

+6
-1
lines changed

xarray/core/dataarray.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,11 @@ def _infer_coords_and_dims(
113113
coord = as_variable(coord, name=dims[n]).to_index_variable()
114114
dims[n] = coord.name
115115
dims = tuple(dims)
116+
elif len(dims) != len(shape):
117+
raise ValueError(
118+
"different number of dimensions on data "
119+
"and dims: %s vs %s" % (len(shape), len(dims))
120+
)
116121
else:
117122
for d in dims:
118123
if not isinstance(d, str):

xarray/tests/test_dataarray.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1468,7 +1468,7 @@ def test_init_value(self):
14681468
actual = DataArray(coords=[("x", np.arange(10)), ("y", ["a", "b"])])
14691469
assert_identical(expected, actual)
14701470

1471-
with pytest.raises(KeyError):
1471+
with raises_regex(ValueError, "different number of dim"):
14721472
DataArray(np.array(1), coords={"x": np.arange(10)}, dims=["x"])
14731473
with raises_regex(ValueError, "does not match the 0 dim"):
14741474
DataArray(np.array(1), coords=[("x", np.arange(10))])

0 commit comments

Comments
 (0)