Skip to content

Commit 9cffe1f

Browse files
authored
Validate shape of DataArray coords with same name as dimensions (#1710)
Fixes GH1709
1 parent 014b789 commit 9cffe1f

File tree

4 files changed

+17
-4
lines changed

4 files changed

+17
-4
lines changed

doc/whats-new.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@ Bug fixes
5858
(:issue:`1697`).
5959
By `Stephan Hoyer <https://github.com/shoyer>`_.
6060

61+
- Validate the shape of coordinates with names matching dimensions in the
62+
DataArray constructor (:issue:`1709`).
63+
By `Stephan Hoyer <https://github.com/shoyer>`_.
64+
6165
Testing
6266
~~~~~~~
6367

xarray/core/dataarray.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def _infer_coords_and_dims(shape, coords, dims):
3535
"""All the logic for creating a new DataArray"""
3636

3737
if (coords is not None and not utils.is_dict_like(coords) and
38-
len(coords) != len(shape)):
38+
len(coords) != len(shape)):
3939
raise ValueError('coords is not dict-like, but it has %s items, '
4040
'which does not match the %s dimensions of the '
4141
'data' % (len(coords), len(shape)))
@@ -50,8 +50,8 @@ def _infer_coords_and_dims(shape, coords, dims):
5050
if utils.is_dict_like(coords):
5151
# deprecated in GH993, removed in GH1539
5252
raise ValueError('inferring DataArray dimensions from '
53-
'dictionary like ``coords`` has been '
54-
'deprecated. Use an explicit list of '
53+
'dictionary like ``coords`` is no longer '
54+
'supported. Use an explicit list of '
5555
'``dims`` instead.')
5656
for n, (dim, coord) in enumerate(zip(dims, coords)):
5757
coord = as_variable(coord,
@@ -87,6 +87,12 @@ def _infer_coords_and_dims(shape, coords, dims):
8787
'length %s on the data but length %s on '
8888
'coordinate %r' % (d, sizes[d], s, k))
8989

90+
if k in sizes and v.shape != (sizes[k],):
91+
raise ValueError('coordinate %r is a DataArray dimension, but '
92+
'it has shape %r rather than expected shape %r '
93+
'matching the dimension size'
94+
% (k, v.shape, (sizes[k],)))
95+
9096
assert_unique_multiindex_level_names(new_coords)
9197

9298
return new_coords, dims

xarray/core/variable.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ def as_variable(obj, name=None):
9696
'{}'.format(obj))
9797
elif utils.is_scalar(obj):
9898
obj = Variable([], obj)
99-
elif (isinstance(obj, (pd.Index, IndexVariable)) and obj.name is not None):
99+
elif isinstance(obj, (pd.Index, IndexVariable)) and obj.name is not None:
100100
obj = Variable(obj.name, obj)
101101
elif name is not None:
102102
data = as_compatible_data(obj)

xarray/tests/test_dataarray.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,9 @@ def test_constructor_invalid(self):
298298
DataArray(np.random.rand(4, 4),
299299
[('x', self.mindex), ('level_1', range(4))])
300300

301+
with raises_regex(ValueError, 'matching the dimension size'):
302+
DataArray(data, coords={'x': 0}, dims=['x', 'y'])
303+
301304
def test_constructor_from_self_described(self):
302305
data = [[-0.1, 21], [0, 2]]
303306
expected = DataArray(data,

0 commit comments

Comments
 (0)