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

WIP: don't create indexes on multidimensional dimensions #2405

Closed
Closed
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
2 changes: 1 addition & 1 deletion xarray/core/coordinates.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion xarray/core/formatting.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion xarray/core/indexes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
12 changes: 5 additions & 7 deletions xarray/core/variable.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
26 changes: 24 additions & 2 deletions xarray/tests/test_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__()
Expand Down Expand Up @@ -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'∑'})
Expand Down Expand Up @@ -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'):
Expand All @@ -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):
Expand Down