Skip to content

Commit 4295b93

Browse files
authored
Better error message when assigning coordinates without dimensions (#971)
* Better error message when assigning coordinates without dimensions Fixes GH969 * revert getattr change * slightly better message
1 parent 0ddca91 commit 4295b93

File tree

3 files changed

+16
-7
lines changed

3 files changed

+16
-7
lines changed

doc/whats-new.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ Enhancements
3333
By `Guido Imperiale <https://github.com/crusaderky>`_.
3434
- :py:func:`~xarray.broadcast` and :py:func:`~xarray.concat` will now auto-align inputs,
3535
using ``join=outer``. By `Guido Imperiale <https://github.com/crusaderky>`_.
36+
- Better error message when assigning variables without dimensions
37+
(:issue:`971`). By `Stephan Hoyer <https://github.com/shoyer>`_.
3638

3739
Bug fixes
3840
~~~~~~~~~

xarray/core/variable.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,17 +58,25 @@ def as_variable(obj, name=None, copy=False):
5858
elif getattr(obj, 'name', None) is not None:
5959
obj = Variable(obj.name, obj)
6060
elif name is not None:
61-
obj = Variable(name, obj)
61+
data = as_compatible_data(obj)
62+
if data.ndim != 1:
63+
raise ValueError(
64+
'cannot set variable %r with %r-dimensional data '
65+
'without explicit dimension names. Pass a tuple of '
66+
'(dims, data) instead.' % (name, data.ndim))
67+
obj = Variable(name, obj, fastpath=True)
6268
else:
6369
raise TypeError('unable to convert object into a variable without an '
6470
'explicit list of dimensions: %r' % obj)
6571

6672
if name is not None and name in obj.dims:
6773
# convert the into an Index
6874
if obj.ndim != 1:
69-
raise ValueError('the variable %r has the same name as one of its '
70-
'dimensions %r, but it is not 1-dimensional and '
71-
'thus it is not a valid index' % (name, obj.dims))
75+
raise ValueError(
76+
'%r has more than 1-dimension and the same name as one of its '
77+
'dimensions %r. xarray disallows such variables because they '
78+
'conflict with the coordinates used to label dimensions.'
79+
% (name, obj.dims))
7280
obj = obj.to_coord()
7381

7482
return obj

xarray/test/test_dataset.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -141,8 +141,7 @@ def test_constructor(self):
141141

142142
with self.assertRaisesRegexp(ValueError, 'conflicting sizes'):
143143
Dataset({'a': x1, 'b': x2})
144-
with self.assertRaisesRegexp(ValueError,
145-
"variable 'x' has the same name"):
144+
with self.assertRaisesRegexp(ValueError, "disallows such variables"):
146145
Dataset({'a': x1, 'x': z})
147146
with self.assertRaisesRegexp(TypeError, 'tuples to convert'):
148147
Dataset({'x': (1, 2, 3, 4, 5, 6, 7)})
@@ -1484,7 +1483,7 @@ def test_setitem(self):
14841483
self.assertDatasetIdentical(data1, data2)
14851484
# can't assign an ND array without dimensions
14861485
with self.assertRaisesRegexp(ValueError,
1487-
'dimensions .* must have the same len'):
1486+
'without explicit dimension names'):
14881487
data2['C'] = var.values.reshape(2, 4)
14891488
# but can assign a 1D array
14901489
data1['C'] = var.values

0 commit comments

Comments
 (0)