diff --git a/xarray/core/coordinates.py b/xarray/core/coordinates.py index 820937dae6a..e5a697f171f 100644 --- a/xarray/core/coordinates.py +++ b/xarray/core/coordinates.py @@ -10,7 +10,7 @@ expand_and_merge_variables, merge_coords, merge_coords_for_inplace_math) from .pycompat import OrderedDict from .utils import Frozen, ReprObject, either_dict_or_kwargs -from .variable import Variable +from .variable import Variable, IndexVariable # Used as the key corresponding to a DataArray's variable when converting # arbitrary DataArray objects to datasets diff --git a/xarray/core/formatting.py b/xarray/core/formatting.py index 50fa64c9987..f68076c0732 100644 --- a/xarray/core/formatting.py +++ b/xarray/core/formatting.py @@ -272,7 +272,7 @@ def summarize_datavar(name, var, col_width): def summarize_coord(name, var, col_width): - is_index = name in var.dims + is_index = name in var.dims and var.ndim==1 show_values = var._in_memory marker = u'*' if is_index else u' ' if is_index: diff --git a/xarray/core/indexes.py b/xarray/core/indexes.py index ffa483fc370..8ac37f03e4d 100644 --- a/xarray/core/indexes.py +++ b/xarray/core/indexes.py @@ -52,4 +52,4 @@ def default_indexes(coords, dims): to indexes used for indexing along that dimension. """ return OrderedDict((key, coords[key].to_index()) - for key in dims if key in coords) + for key in dims if key in coords and coords[key].ndim==1) diff --git a/xarray/core/variable.py b/xarray/core/variable.py index 8bd7225efc3..7b75d242985 100644 --- a/xarray/core/variable.py +++ b/xarray/core/variable.py @@ -105,13 +105,11 @@ def as_variable(obj, name=None): if name is not None and name in obj.dims: # convert the Variable into an Index - if obj.ndim != 1: - raise MissingDimensionsError( - '%r has more than 1-dimension and the same name as one of its ' - 'dimensions %r. xarray disallows such variables because they ' - 'conflict with the coordinates used to label ' - 'dimensions.' % (name, obj.dims)) - obj = obj.to_index_variable() + if obj.ndim == 1: + obj = obj.to_index_variable() + else: + pass + # TODO: do something else with multidimensional variables return obj diff --git a/xarray/tests/test_dataset.py b/xarray/tests/test_dataset.py index e55caf1bf13..5bc9489c553 100644 --- a/xarray/tests/test_dataset.py +++ b/xarray/tests/test_dataset.py @@ -60,6 +60,11 @@ def create_test_multiindex(): return Dataset({}, {'x': mindex}) +def create_test_multidim_coord_no_index(): + ds = xr.Dataset({'a': ('x', [1, 1]), + 'x': (['x', 'y'], [[1, 1, 1, 1], [2, 2, 2, 2]])}) + return ds + class InaccessibleVariableDataStore(backends.InMemoryDataStore): def __init__(self): super(InaccessibleVariableDataStore, self).__init__() @@ -176,6 +181,11 @@ def test_repr_period_index(self): # check that creating the repr doesn't raise an error #GH645 repr(data) + def test_repr_no_index_on_multidim_coord(self): + data = create_test_multidim_coord_no_index() + actual = repr(data) + assert '*' not in actual + def test_unicode_data(self): # regression test for GH834 data = Dataset({u'foø': [u'ba®']}, attrs={u'å': u'∑'}) @@ -237,8 +247,9 @@ def test_constructor(self): with raises_regex(ValueError, 'conflicting sizes'): Dataset({'a': x1, 'b': x2}) - with raises_regex(ValueError, "disallows such variables"): - Dataset({'a': x1, 'x': z}) + # these are now allowed -- let's add a more explicit test + #with raises_regex(ValueError, "disallows such variables"): + # Dataset({'a': x1, 'x': z}) with raises_regex(TypeError, 'tuple of form'): Dataset({'x': (1, 2, 3, 4, 5, 6, 7)}) with raises_regex(ValueError, 'already exists as a scalar'): @@ -249,6 +260,17 @@ def test_constructor(self): actual = Dataset({'z': expected['z']}) assert_identical(expected, actual) + def test_constructor_multidim_dimensions(self): + # checks related to GH2368, GH2233 + ds = create_test_multidim_coord_no_index() + + # dataset should have no indices + assert not isinstance(ds.variables['x'], IndexVariable) + assert len(ds.indexes) == 0 + assert 'x' not in ds.indexes + with pytest.raises(KeyError): + ds.indexes['x'] + def test_constructor_invalid_dims(self): # regression for GH1120 with pytest.raises(MergeError):