Skip to content

fix bugs in is_scalar and as_variable for dask arrays #1685

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

Merged
merged 5 commits into from
Nov 5, 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
5 changes: 4 additions & 1 deletion doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,12 @@ Bug fixes
~~~~~~~~~

- Suppress warning in Ipython autocompletion, related to the deprecation
of ``.T`` attributes. (:issue:`1675`).
of ``.T`` attributes (:issue:`1675`).
By `Keisuke Fujii <https://github.com/fujiisoup>`_

- Fix two bugs that were preventing dask arrays from being specified as
coordinates in the DataArray constructor (:issue:`1684`).
By `Joe Hamman <https://github.com/jhamman>`_

v0.10.0 rc1 (30 October 2017)
-----------------------------
Expand Down
5 changes: 3 additions & 2 deletions xarray/core/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
import numpy as np
import pandas as pd

from .pycompat import iteritems, OrderedDict, basestring, bytes_type
from .pycompat import (iteritems, OrderedDict, basestring, bytes_type,
dask_array_type)


def alias_message(old_name, new_name):
Expand Down Expand Up @@ -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))


def is_valid_numpy_dtype(dtype):
Expand Down
2 changes: 1 addition & 1 deletion xarray/core/variable.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ 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 obj.name is not None):
obj = Variable(obj.name, obj)
elif name is not None:
data = as_compatible_data(obj)
Expand Down
15 changes: 15 additions & 0 deletions xarray/tests/test_dataarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,21 @@ def test_constructor_from_0d(self):
actual = DataArray(0)
self.assertDataArrayIdentical(expected, actual)

@requires_dask
def test_constructor_dask_coords(self):
# regression test for GH1684
import dask.array as da

coord = da.arange(8, chunks=(4,))
data = da.random.random((8, 8), chunks=(4, 4)) + 1
actual = DataArray(data, coords={'x': coord, 'y': coord},
dims=['x', 'y'])

ecoord = np.arange(8)
expected = DataArray(data, coords={'x': ecoord, 'y': ecoord},
dims=['x', 'y'])
assert_equal(actual, expected)

def test_equals_and_identical(self):
orig = DataArray(np.arange(5.0), {'a': 42}, dims='x')

Expand Down
11 changes: 10 additions & 1 deletion xarray/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

from xarray.core import duck_array_ops, utils
from xarray.core.pycompat import OrderedDict
from . import TestCase
from . import TestCase, requires_dask


class TestAlias(TestCase):
Expand Down Expand Up @@ -180,3 +180,12 @@ def test_hashable(self):
self.assertTrue(utils.hashable(v))
for v in [[5, 6], ['seven', '8'], {9: 'ten'}]:
self.assertFalse(utils.hashable(v))


@requires_dask
def test_dask_array_is_scalar():
# regression test for GH1684
import dask.array as da

y = da.arange(8, chunks=4)
assert not utils.is_scalar(y)