Closed
Description
Dataset
allows assignment of new variables with dimension names that are used in a MultiIndex, even if the lengths do not match the existing coordinate.
a = pd.DataFrame({'a': [1, 2], 'b': [3, 4]}).unstack('a')
a.index.names = ['dim0', 'dim1']
a.index.name = 'stacked_dim'
b = xr.Dataset(coords={'dim0': ['a', 'b'], 'dim1': [0, 1]})
b = b.stack(dim_stacked=['dim0', 'dim1'])
assert(len(b.dim0) == 4)
# This should raise an errors because the length is != 4
b['c'] = (('dim0',), [10, 11])
b
Instead, it reports dim0
as a new dimension without coordinates:
<xarray.Dataset>
Dimensions: (dim0: 2, dim_stacked: 4)
Coordinates:
* dim_stacked (dim_stacked) MultiIndex
- dim0 (dim_stacked) object 'a' 'a' 'b' 'b'
- dim1 (dim_stacked) int64 0 1 0 1
Dimensions without coordinates: dim0
Data variables:
c (dim0) int64 10 11
Similar cases of coordinates that aren't used do raise an error:
ds = xr.Dataset()
ds.coords['a'] = [1, 2, 3]
ds = ds.sel(a=1)
ds['b'] = (('a',), [1, 2])
ds
Output of xr.show_versions()
INSTALLED VERSIONS
------------------
commit: None
python: 3.6.5.final.0
python-bits: 64
OS: Darwin
OS-release: 17.7.0
machine: x86_64
processor: i386
byteorder: little
LC_ALL: en_GB.UTF-8
LANG: None
LOCALE: en_GB.UTF-8
xarray: 0.10.7
pandas: 0.23.2
numpy: 1.14.5
scipy: 1.1.0
netCDF4: 1.4.0
h5netcdf: None
h5py: 2.8.0
Nio: None
zarr: None
bottleneck: 1.2.1
cyordereddict: None
dask: 0.18.1
distributed: 1.22.0
matplotlib: 2.2.2
cartopy: None
seaborn: 0.8.1
setuptools: 39.2.0
pip: 10.0.1
conda: 4.5.8
pytest: 3.6.2
IPython: 6.4.0
sphinx: 1.7.5