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

Better error message when assigning coordinates without dimensions #971

Merged
merged 3 commits into from
Aug 18, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 2 additions & 0 deletions doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ Enhancements
By `Guido Imperiale <https://github.com/crusaderky>`_.
- :py:func:`~xarray.broadcast` and :py:func:`~xarray.concat` will now auto-align inputs,
using ``join=outer``. By `Guido Imperiale <https://github.com/crusaderky>`_.
- Better error message when assigning variables without dimensions
(:issue:`971`). By `Stephan Hoyer <https://github.com/shoyer>`_.

Bug fixes
~~~~~~~~~
Expand Down
17 changes: 12 additions & 5 deletions xarray/core/variable.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def as_variable(obj, name=None, copy=False):
obj = obj.copy(deep=False)
elif hasattr(obj, 'dims') and (hasattr(obj, 'data') or
hasattr(obj, 'values')):
obj = Variable(obj.dims, getattr(obj, 'data', obj.values),
obj = Variable(obj.dims, getattr(obj, 'data', getattr(obj, 'values')),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Out of interest, why is this better?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually I was confused when I switched this. It does literally the exact
same thing - I will revert. My concern was that this might prematurely
evaluate the data if it's a dask array.
On Tue, Aug 16, 2016 at 9:16 PM Maximilian Roos notifications@github.com
wrote:

In xarray/core/variable.py
#971 (comment):

@@ -42,7 +42,7 @@ def as_variable(obj, name=None, copy=False):
obj = obj.copy(deep=False)
elif hasattr(obj, 'dims') and (hasattr(obj, 'data') or
hasattr(obj, 'values')):

  •    obj = Variable(obj.dims, getattr(obj, 'data', obj.values),
    
  •    obj = Variable(obj.dims, getattr(obj, 'data', getattr(obj, 'values')),
    

Out of interest, why is this better?


You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
https://github.com/pydata/xarray/pull/971/files/25bebc8c427f5947545ba3de101ca02108754037#r75060357,
or mute the thread
https://github.com/notifications/unsubscribe-auth/ABKS1s27hTk1CurNGfup--qbCecMSdZaks5qgosBgaJpZM4JmDcq
.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I think getattr is only useful if you don't know what attr it might be. But no harm!

getattr(obj, 'attrs', None),
getattr(obj, 'encoding', None))
elif isinstance(obj, tuple):
Expand All @@ -58,17 +58,24 @@ def as_variable(obj, name=None, copy=False):
elif getattr(obj, 'name', None) is not None:
obj = Variable(obj.name, obj)
elif name is not None:
obj = Variable(name, obj)
data = as_compatible_data(obj)
if data.ndim != 1:
raise ValueError(
'cannot set variable %r with %r-dimensional data '
'without explicit dimension names. Pass a tuple of '
'(dims, data) instead.' % (name, data.ndim))
obj = Variable(name, obj, fastpath=True)
else:
raise TypeError('unable to convert object into a variable without an '
'explicit list of dimensions: %r' % obj)

if name is not None and name in obj.dims:
# convert the into an Index
if obj.ndim != 1:
raise ValueError('the variable %r has the same name as one of its '
'dimensions %r, but it is not 1-dimensional and '
'thus it is not a valid index' % (name, obj.dims))
raise ValueError(
'%r has more than 1-dimension and the same name as one of its '
'dimensions %r. xarray disallows such variables because they '
'conflict with dimension coordinates.' % (name, obj.dims))
obj = obj.to_coord()

return obj
Expand Down
5 changes: 2 additions & 3 deletions xarray/test/test_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,7 @@ def test_constructor(self):

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