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

Confusing behaviour with MultiIndex #2299

Closed
aseyboldt opened this issue Jul 18, 2018 · 1 comment
Closed

Confusing behaviour with MultiIndex #2299

aseyboldt opened this issue Jul 18, 2018 · 1 comment
Assignees
Labels

Comments

@aseyboldt
Copy link

aseyboldt commented Jul 18, 2018

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
@fujiisoup
Copy link
Member

fujiisoup commented Jul 18, 2018

Thanks for raising an issue, with a reproduceable example.
This looks a bug. It looks we just skip the duplicate check of the dimension names which are level variables of MultiIndex.

After the assignment, b['dim0'] returns [0, 1] not a level variable of the MultiIndex.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants