-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
fix bugs in is_scalar and as_variable for dask arrays #1685
Conversation
xarray/core/utils.py
Outdated
@@ -188,7 +189,7 @@ def is_scalar(value): | |||
return ( | |||
getattr(value, 'ndim', None) == 0 or | |||
isinstance(value, (basestring, bytes_type)) or not | |||
isinstance(value, Iterable)) | |||
isinstance(value, (Iterable, dask_array_type))) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
dask_array_type
is actually a tuple, so this needs to be (Iterable,) + dask_array_type
.
That said... this already tricky logic is getting even more convoluted. So this isn't my favorite fix, though I agree that an N-dimensional dask array should not be considered a scalar.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What would be a preferable fix here? It may be worth going back and seeing what the numpy array track is in as_variable
.
xarray/core/variable.py
Outdated
@@ -94,7 +94,8 @@ def as_variable(obj, name=None): | |||
'{}'.format(obj)) | |||
elif utils.is_scalar(obj): | |||
obj = Variable([], obj) | |||
elif getattr(obj, 'name', None) is not None: | |||
elif (getattr(obj, 'name', None) is not None and not |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead, let's call out the types from which we can safely get the name for the dimension (there are likely to be other types that reuse name
). I think that's IndexVariable
and pandas.Index
.
xarray/core/variable.py
Outdated
@@ -94,7 +94,8 @@ def as_variable(obj, name=None): | |||
'{}'.format(obj)) | |||
elif utils.is_scalar(obj): | |||
obj = Variable([], obj) | |||
elif getattr(obj, 'name', None) is not None: | |||
elif (isinstance(obj, (pd.Index, IndexVariable)) and | |||
getattr(obj, 'name', None) is not None): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This can just be obj.name is not None
(since these objects are guaranteed to have name
attributes)
git diff upstream/master **/*py | flake8 --diff
whats-new.rst
for all changes andapi.rst
for new APIxref: #1674
cc @shoyer, @mrocklin