You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
It is currently possible to create a Coordinates object where a variable shares a name with a dimension, but the variable is not 1D. This is explicitly forbidden by the xarray data model.
What did you expect to happen?
If you try to pass the resulting object into the Dataset constructor you get the expected error telling you that this is forbidden, but that error should have been raised by Coordinates.__init__.
Minimal Complete Verifiable Example
In [1]: fromxarray.core.coordinatesimportCoordinatesIn [2]: fromxarray.core.variableimportVariableIn [4]: importnumpyasnpIn [5]: var=Variable(data=np.arange(6).reshape(2, 3), dims=['x', 'y'])
In [6]: varOut[6]:
<xarray.Variable (x: 2, y: 3)>Size: 48Barray([[0, 1, 2],
[3, 4, 5]])
In [7]: coords=Coordinates(coords={'x': var}, indexes={})
In [8]: coordsOut[8]:
Coordinates:
x (x, y) int6448B012345In [10]: importxarrayasxrIn [11]: ds=xr.Dataset(coords=coords)
---------------------------------------------------------------------------MergeErrorTraceback (mostrecentcalllast)
CellIn[11], line1---->1ds=xr.Dataset(coords=coords)
File~/Documents/Work/Code/xarray/xarray/core/dataset.py:693, inDataset.__init__(self, data_vars, coords, attrs)
690ifisinstance(coords, Dataset):
691coords=coords._variables-->693variables, coord_names, dims, indexes, _=merge_data_and_coords(
694data_vars, coords695 )
697self._attrs=dict(attrs) ifattrselseNone698self._close=NoneFile~/Documents/Work/Code/xarray/xarray/core/dataset.py:422, inmerge_data_and_coords(data_vars, coords)
418coords=create_coords_with_default_indexes(coords, data_vars)
420# exclude coords from alignment (all variables in a Coordinates object should421# already be aligned together) and use coordinates' indexes to align data_vars-->422returnmerge_core(
423 [data_vars, coords],
424compat="broadcast_equals",
425join="outer",
426explicit_coords=tuple(coords),
427indexes=coords.xindexes,
428priority_arg=1,
429skip_align_args=[1],
430 )
File~/Documents/Work/Code/xarray/xarray/core/merge.py:731, inmerge_core(objects, compat, join, combine_attrs, priority_arg, explicit_coords, indexes, fill_value, skip_align_args)
729coord_names.intersection_update(variables)
730ifexplicit_coordsisnotNone:
-->731assert_valid_explicit_coords(variables, dims, explicit_coords)
732coord_names.update(explicit_coords)
733fordim, sizeindims.items():
File~/Documents/Work/Code/xarray/xarray/core/merge.py:577, inassert_valid_explicit_coords(variables, dims, explicit_coords)
575forcoord_nameinexplicit_coords:
576ifcoord_nameindimsandvariables[coord_name].dims!= (coord_name,):
-->577raiseMergeError(
578f"coordinate {coord_name} shares a name with a dataset dimension, but is "579"not a 1D variable along that dimension. This is disallowed "580"by the xarray data model."581 )
MergeError: coordinatexsharesanamewithadatasetdimension, butisnota1Dvariablealongthatdimension. Thisisdisallowedbythexarraydatamodel.
MVCE confirmation
Minimal example — the example is as focused as reasonably possible to demonstrate the underlying issue in xarray.
Complete example — the example is self-contained, including all data and the text of any traceback.
Verifiable example — the example copy & pastes into an IPython prompt or Binder notebook, returning the result.
New issue — a search of GitHub Issues suggests this is not a duplicate.
Recent environment — the issue occurs with the latest version of xarray and its dependencies.
It is currently possible to create a Coordinates object where a variable shares a name with a dimension, but the variable is not 1D. This is explicitly forbidden by the xarray data model.
Shouldn't we allow this instead of raising an error? It is now allowed by the xarray data model and supported when opening a dataset from a file (#7989).
What happened?
It is currently possible to create a
Coordinates
object where a variable shares a name with a dimension, but the variable is not 1D. This is explicitly forbidden by the xarray data model.What did you expect to happen?
If you try to pass the resulting object into the
Dataset
constructor you get the expected error telling you that this is forbidden, but that error should have been raised byCoordinates.__init__
.Minimal Complete Verifiable Example
MVCE confirmation
Relevant log output
No response
Anything else we need to know?
I noticed this whilst working on #8872
Environment
main
The text was updated successfully, but these errors were encountered: