Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validate shape of DataArray coords with same name as dimensions #1710

Merged
merged 1 commit into from
Nov 12, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ Bug fixes
(:issue:`1697`).
By `Stephan Hoyer <https://github.com/shoyer>`_.

- Validate the shape of coordinates with names matching dimensions in the
DataArray constructor (:issue:`1709`).
By `Stephan Hoyer <https://github.com/shoyer>`_.

Testing
~~~~~~~

Expand Down
12 changes: 9 additions & 3 deletions xarray/core/dataarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def _infer_coords_and_dims(shape, coords, dims):
"""All the logic for creating a new DataArray"""

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

if k in sizes and v.shape != (sizes[k],):
raise ValueError('coordinate %r is a DataArray dimension, but '
'it has shape %r rather than expected shape %r '
'matching the dimension size'
% (k, v.shape, (sizes[k],)))

assert_unique_multiindex_level_names(new_coords)

return new_coords, dims
Expand Down
2 changes: 1 addition & 1 deletion xarray/core/variable.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ def as_variable(obj, name=None):
'{}'.format(obj))
elif utils.is_scalar(obj):
obj = Variable([], obj)
elif (isinstance(obj, (pd.Index, IndexVariable)) and obj.name is not None):
elif isinstance(obj, (pd.Index, IndexVariable)) and obj.name is not None:
obj = Variable(obj.name, obj)
elif name is not None:
data = as_compatible_data(obj)
Expand Down
3 changes: 3 additions & 0 deletions xarray/tests/test_dataarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,9 @@ def test_constructor_invalid(self):
DataArray(np.random.rand(4, 4),
[('x', self.mindex), ('level_1', range(4))])

with raises_regex(ValueError, 'matching the dimension size'):
DataArray(data, coords={'x': 0}, dims=['x', 'y'])

def test_constructor_from_self_described(self):
data = [[-0.1, 21], [0, 2]]
expected = DataArray(data,
Expand Down