Skip to content

Request error handling in dataarray construction #3437

@rpgoldman

Description

@rpgoldman

When I have a bad dimension in an xarray I get this error:

ValueError: different number of dimensions on data and dims: 2 vs 3

Expected Output

what I would like to see is

ValueError: different number of dimensions on data and dims: 2 vs 3 for variable Foo

The improvement I am asking for is here:

data = _check_data_shape(data, coords, dims)
data = as_compatible_data(data)
coords, dims = _infer_coords_and_dims(data.shape, coords, dims)
variable = Variable(dims, data, attrs, encoding, fastpath=True)

What I claim would be an improvement would be something like this:

            data = _check_data_shape(data, coords, dims)
            data = as_compatible_data(data)
            try:
                 coords, dims = _infer_coords_and_dims(data.shape, coords, dims)
            except Exception as e:
                 if name:
                     raise Exception("Error finding coordinates and dims for variable %s: %s"%(name, e)
                 else:
                    raise e
            variable = Variable(dims, data, attrs, encoding, fastpath=True)

I'm not an expert on python exception handling, so this is probably wrong.

Problem Description

The programmer cannot tell what variable causes the shape and dimension issue.

Output of xr.show_versions()

python: 3.6.8 (default, Sep 6 2019, 11:45:11) [GCC 4.2.1 Compatible Apple LLVM 10.0.1 (clang-1001.0.46.4)] python-bits: 64 OS: Darwin OS-release: 18.7.0 machine: x86_64 processor: i386 byteorder: little LC_ALL: None LANG: en_US.UTF-8 LOCALE: en_US.UTF-8 libhdf5: 1.10.2 libnetcdf: 4.6.3

xarray: 0.13.0
pandas: 0.25.2
numpy: 1.17.3
scipy: 1.3.1
netCDF4: 1.5.2
pydap: None
h5netcdf: None
h5py: 2.10.0
Nio: None
zarr: None
cftime: 1.0.3.4
nc_time_axis: None
PseudoNetCDF: None
rasterio: None
cfgrib: None
iris: None
bottleneck: 1.2.1
dask: None
distributed: None
matplotlib: 3.1.1
cartopy: None
seaborn: 0.9.0
numbagg: None
setuptools: 41.2.0
pip: 19.3.1
conda: None
pytest: 5.2.0
IPython: 7.8.0
sphinx: None

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions