pydata / pydata/xarray

Inconsistent errors in coord construction from list of tuples vs dict

Open
#6,362 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

What happened?

Using a DataArray as coordinate values for a new DataArray fails when the input is a list of tuples.

What did you expect to happen?

I would expect the construction to succeed, like it does when passed a dict of coordinates.

Minimal Complete Verifiable Example
import xarray as xr

a = xr.DataArray(range(5), coords=[("a", range(5))])

succeeds = xr.DataArray(range(5), coords=dict([("a", a.coords["a"])]))

fails = xr.DataArray(range(5), coords=[("a", a.coords["a"])])
Relevant log output
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "PREFIX/lib/python3.10/site-packages/xarray/core/dataarray.py", line 402, in __init__
    coords, dims = _infer_coords_and_dims(data.shape, coords, dims)
  File "PREFIX/lib/python3.10/site-packages/xarray/core/dataarray.py", line 117, in _infer_coords_and_dims
    coord = as_variable(coord, name=dims[n]).to_index_variable()
  File "PREFIX/site-packages/xarray/core/variable.py", line 112, in as_variable
    raise TypeError(
TypeError: Using a DataArray object to construct a variable is ambiguous, please extract the data using the .data property.
Anything else we need to know?

No response

Environment

INSTALLED VERSIONS
------------------
commit: None
python: 3.10.2 | packaged by conda-forge | (main, Mar  8 2022, 15:53:57) [GCC 9.4.0]
python-bits: 64
OS: Linux
OS-release: 3.10.0-1160.53.1.el7.x86_64
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: en_US.UTF-8
LANG: en_US.UTF-8
LOCALE: ('en_US', 'UTF-8')
libhdf5: None
libnetcdf: None

xarray: 2022.3.0
pandas: 1.4.1
numpy: 1.22.3
scipy: None
netCDF4: None
pydap: None
h5netcdf: None
h5py: None
Nio: None
zarr: None
cftime: None
nc_time_axis: None
PseudoNetCDF: None
rasterio: None
cfgrib: None
iris: None
bottleneck: None
dask: None
distributed: None
matplotlib: None
cartopy: None
seaborn: None
numbagg: None
fsspec: None
cupy: None
pint: None
sparse: None
setuptools: 60.9.3
pip: 22.0.4
conda: None
pytest: None
IPython: 8.0.1
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

Run the minimal example first, then trace the failure through xarray/core/dataarray.py and xarray/core/variable.py, especially _infer_coords_and_dims and as_variable. Compare the list-of-tuples and dict coordinate paths, and verify that both constructions succeed with regression coverage for the reported DataArray input.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.