Skip to content

combine complementary DataArrays #2095

Closed

Description

@mschrimpf

I have a list of DataArrays with three dimensions.
For each item in the list, two of the dimensions are a single value but the combination of all items would yield the full combinatorial values.

Code Sample

    import itertools
    import numpy as np
    import xarray as xr
    
    ds = []
    for vals_dim1, vals_dim2 in itertools.product(list(range(2)), list(range(3))):
        d = xr.DataArray(np.random.rand(1, 1, 4),
                         coords={'dim1': [vals_dim1], 'dim2': [vals_dim2], 'dim3': range(4)},
                         dims=['dim1', 'dim2', 'dim3'])
        ds.append(d)

Expected Output

I then want to combine these complimentary DataArrays but none of what I tried so far seems to work.
The result should be a DataArray with shape |2x3x4| and dimensions dim1: |2|, dim2: |3|, dim3: |4|.

The following do not work:

    # does not automatically infer dimensions and fails with
    # "ValueError: conflicting sizes for dimension 'concat_dim': length 2 on 'concat_dim' and length 6 on <this-array>"
    ds = xr.concat(ds, dim=['dim1', 'dim2'])
    
    # will still try to insert a new `concat_dim` and fails with
    # "ValueError: conflicting MultiIndex level name(s): 'dim1' (concat_dim), (dim1) 'dim2' (concat_dim), (dim2)"
    import pandas as pd
    dims = [[0] * 3 + [1] * 3, list(range(3)) * 2]
    dims = pd.MultiIndex.from_arrays(dims, names=['dim1', 'dim2'])
    ds = xr.concat(ds, dim=dims)
    
    # fails with
    # AttributeError: 'DataArray' object has no attribute 'data_vars'
    ds = xr.auto_combine(ds)

Output of xr.show_versions()

Details INSTALLED VERSIONS ------------------ commit: None python: 3.6.4.final.0 python-bits: 64 OS: Linux OS-release: 4.4.0-43-Microsoft machine: x86_64 processor: x86_64 byteorder: little LC_ALL: None LANG: None LOCALE: None.None xarray: 0.10.2 pandas: 0.22.0 numpy: 1.14.2 scipy: 1.0.0 netCDF4: 1.3.1 h5netcdf: None h5py: None Nio: None zarr: None bottleneck: None cyordereddict: None dask: None distributed: None matplotlib: None cartopy: None seaborn: None setuptools: 38.5.1 pip: 10.0.1 conda: None pytest: 3.4.2 IPython: 6.2.1 sphinx: None

Activity

  1. shoyer commented on May 2, 2018

    @shoyer
    Member

    I answered on your StackOverflow post: https://stackoverflow.com/questions/50095019/combine-complementary-dataarrays

    Let me know if that makes sense -- this is legitimately a little trickier than it should be.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions