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

concat fails with attrs that are dictionaries with ndarrays #2575

Closed
griffinmilsap opened this issue Nov 26, 2018 · 1 comment · Fixed by #3637
Closed

concat fails with attrs that are dictionaries with ndarrays #2575

griffinmilsap opened this issue Nov 26, 2018 · 1 comment · Fixed by #3637

Comments

@griffinmilsap
Copy link

griffinmilsap commented Nov 26, 2018

Code Sample

import numpy as np
import xarray as xr

arrs = [
    xr.DataArray( [ [1], [2] ], 
                 dims = [ 'x', 'y' ], 
                 attrs = { 'meta': { 'bar': np.array( [ 10, 20, 30 ] ) } } ),
    xr.DataArray( [ [3], [4] ],
                 dims = [ 'x', 'y' ],
                 attrs = { 'meta': { 'bar': np.array( [ 10, 20, 30 ] ) } } )
]
print( arrs[0] )
print( arrs[1] )
print( xr.concat( arrs, dim = 'y' ) )

Fails with the following error:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-117-9d4c21b40356> in <module>
      9 print( arrs[0] )
     10 print( arrs[1] )
---> 11 print( xr.concat( arrs, dim = 'y' ) )

/usr/local/lib/python3.6/dist-packages/xarray/core/combine.py in concat(objs, dim, data_vars, coords, compat, positions, indexers, mode, concat_over)
    118         raise TypeError('can only concatenate xarray Dataset and DataArray '
    119                         'objects, got %s' % type(first_obj))
--> 120     return f(objs, dim, data_vars, coords, compat, positions)
    121 
    122 

/usr/local/lib/python3.6/dist-packages/xarray/core/combine.py in _dataarray_concat(arrays, dim, data_vars, coords, compat, positions)
    337 
    338     ds = _dataset_concat(datasets, dim, data_vars, coords, compat,
--> 339                          positions)
    340     return arrays[0]._from_temp_dataset(ds, name)
    341 

/usr/local/lib/python3.6/dist-packages/xarray/core/combine.py in _dataset_concat(datasets, dim, data_vars, coords, compat, positions)
    303         if k in concat_over:
    304             vars = ensure_common_dims([ds.variables[k] for ds in datasets])
--> 305             combined = concat_vars(vars, dim, positions)
    306             insert_result_variable(k, combined)
    307 

/usr/local/lib/python3.6/dist-packages/xarray/core/variable.py in concat(variables, dim, positions, shortcut)
   1964         return IndexVariable.concat(variables, dim, positions, shortcut)
   1965     else:
-> 1966         return Variable.concat(variables, dim, positions, shortcut)
   1967 
   1968 

/usr/local/lib/python3.6/dist-packages/xarray/core/variable.py in concat(cls, variables, dim, positions, shortcut)
   1417                 if var.dims != first_var.dims:
   1418                     raise ValueError('inconsistent dimensions')
-> 1419                 utils.remove_incompatible_items(attrs, var.attrs)
   1420 
   1421         return cls(dims, data, attrs, encoding)

/usr/local/lib/python3.6/dist-packages/xarray/core/utils.py in remove_incompatible_items(first_dict, second_dict, compat)
    174         if (k not in second_dict or
    175             (k in second_dict and
--> 176                 not compat(first_dict[k], second_dict[k]))):
    177             del first_dict[k]
    178 

/usr/local/lib/python3.6/dist-packages/xarray/core/utils.py in equivalent(first, second)
    122     else:
    123         return ((first is second) or
--> 124                 (first == second) or
    125                 (pd.isnull(first) and pd.isnull(second)))
    126 

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

Problem description

This is a problem because the following code actually executes properly

import numpy as np
import xarray as xr
arrs = [
    xr.DataArray( [ [1], [2] ], 
                 dims = [ 'x', 'y' ], 
                 attrs = { 'meta': np.array( [ 10, 20, 30 ] ) } ),
    xr.DataArray( [ [3], [4] ],
                 dims = [ 'x', 'y' ],
                 attrs = { 'meta': np.array( [ 10, 20, 30 ] ) } )
]
print( arrs[0] )
print( arrs[1] )
print( xr.concat( arrs, dim = 'y' ) )
<xarray.DataArray (x: 2, y: 1)>
array([[1],
       [2]])
Dimensions without coordinates: x, y
Attributes:
    meta:     [10 20 30]
<xarray.DataArray (x: 2, y: 1)>
array([[3],
       [4]])
Dimensions without coordinates: x, y
Attributes:
    meta:     [10 20 30]
<xarray.DataArray (x: 2, y: 2)>
array([[1, 3],
       [2, 4]])
Dimensions without coordinates: x, y
Attributes:
    meta:     [10 20 30]

Equivalence for an array within a nested dictionary as an attribute is evaluated differently than an array attribute, which is non-intuitive. This bug is related to #2060 but is additionally pointing out a difference in evaluation for more complex attributes.

Expected Output

The output of the code sample should concatenate successfully with the nested dictionary attribute, or a more easily interpretable error should be thrown telling me I'm dumb for using dictionaries in attributes. (See #2060)

Output of xr.show_versions()

INSTALLED VERSIONS

commit: None
python: 3.6.6.final.0
python-bits: 64
OS: Linux
OS-release: 4.15.0-23-generic
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8
LOCALE: en_US.UTF-8

xarray: 0.10.9
pandas: 0.23.4
numpy: 1.15.3
scipy: 1.1.0
netCDF4: None
h5netcdf: None
h5py: None
Nio: None
zarr: None
cftime: None
PseudonetCDF: None
rasterio: None
iris: None
bottleneck: None
cyordereddict: None
dask: None
distributed: None
matplotlib: 3.0.0
cartopy: None
seaborn: None
setuptools: 40.4.3
pip: 9.0.1
conda: None
pytest: None
IPython: 7.0.1
sphinx: None

@shoyer
Copy link
Member

shoyer commented Nov 26, 2018

See my comment on #2060. I think we should probably drop this attribute check all together.

Any interest in putting together a PR?

dcherian added a commit to dcherian/xarray that referenced this issue Dec 17, 2019
dcherian added a commit to dcherian/xarray that referenced this issue Dec 17, 2019
dcherian added a commit to dcherian/xarray that referenced this issue Dec 17, 2019
TomNicholas pushed a commit that referenced this issue Dec 24, 2019
* concat keep attrs from first variable.

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

Successfully merging a pull request may close this issue.

3 participants