Skip to content

Fix/1120 #1538

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 8 commits into from
Sep 6, 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
3 changes: 3 additions & 0 deletions doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,9 @@ Bug fixes
objects with data stored as ``dask`` arrays (:issue:`1529`).
By `Joe Hamman <https://github.com/jhamman>`_.

- ``:py:meth:`~xarray.Dataset.__init__` raises a ``MergeError`` if an
coordinate shares a name with a dimension but is comprised of arbitrary
dimensions(:issue:`1120`).
- :py:func:`~xarray.open_rasterio` method now skips rasterio.crs -attribute if
it is none.
By `Leevi Annala <https://github.com/leevei>`_.
Expand Down
21 changes: 18 additions & 3 deletions xarray/core/merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,20 @@ def merge_data_and_coords(data, coords, compat='broadcast_equals',
return merge_core(objs, compat, join, explicit_coords=explicit_coords)


def assert_valid_explicit_coords(variables, dims, explicit_coords):
"""Validate explicit coordinate names/dims.

Raise a MergeError if an explicit coord shares a name with a dimension
but is comprised of arbitrary dimensions.
"""
for coord_name in explicit_coords:
if coord_name in dims and variables[coord_name].dims != (coord_name,):
raise MergeError(
'coordinate %s shares a name with a dataset dimension, but is '
'not a 1D variable along that dimension. This is disallowed '
'by the xarray data model.' % coord_name)


def merge_core(objs,
compat='broadcast_equals',
join='outer',
Expand Down Expand Up @@ -414,15 +428,16 @@ def merge_core(objs,

coord_names, noncoord_names = determine_coords(coerced)

if explicit_coords is not None:
coord_names.update(explicit_coords)

priority_vars = _get_priority_vars(aligned, priority_arg, compat=compat)
variables = merge_variables(expanded, priority_vars, compat=compat)
assert_unique_multiindex_level_names(variables)

dims = calculate_dimensions(variables)

if explicit_coords is not None:
assert_valid_explicit_coords(variables, dims, explicit_coords)
coord_names.update(explicit_coords)

for dim, size in dims.items():
if dim in variables:
coord_names.add(dim)
Expand Down
6 changes: 6 additions & 0 deletions xarray/tests/test_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,12 @@ def test_constructor(self):
actual = Dataset({'z': expected['z']})
self.assertDatasetIdentical(expected, actual)

def test_constructor_invalid_dims(self):
# regression for GH1120
with self.assertRaises(MergeError):
Dataset(data_vars=dict(v=('y', [1, 2, 3, 4])),
coords=dict(y=DataArray([.1, .2, .3, .4], dims='x')))

def test_constructor_1d(self):
expected = Dataset({'x': (['x'], 5.0 + np.arange(5))})
actual = Dataset({'x': 5.0 + np.arange(5)})
Expand Down