pydata / pydata/xarray

Request error handling in dataarray construction

Open
#3,437 4 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

topic-error reporting
Dominant language
Python
Stars
4.2k
Forks
1.4k
Avg merge
2d 15h
Merged PRs (30d)
14

Description

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: https://github.com/pydata/xarray/blob/c8dac5866d2c54ee6b262b5060a701e0be1e40cb/xarray/core/dataarray.py#L368-L371

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in xarray/core/dataarray.py at the linked dataarray construction code around lines 368-371. Reproduce construction with mismatched data dimensions and dims, then verify that the resulting ValueError identifies the variable name when one is provided. Done means the existing dimension error remains informative and includes the variable context.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
data
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.