Assigning to dataset with missing dim raises ValueError
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 4.2k
- Forks
- 1.4k
- Avg merge
- 2d 15h
- Merged PRs (30d)
- 14
Description
What happened?
I tried to assign values to a dataset with a selector-dict where a variable is missing the dim from the selector-dict.
This raises a ValueError.
What did you expect to happen?
I expect that assigning works the same as selecting and it will ignore the missing dims.
Minimal Complete Verifiable Example
import xarray as xr
ds = xr.Dataset({"a": ("x", [1, 2, 3]), "b": ("y", [4, 5])})
ds[{"x": 1}]
# this works and returns:
# <xarray.Dataset>
# Dimensions: (y: 2)
# Dimensions without coordinates: y
# Data variables:
# a int64 2
# b (y) int64 4 5
ds[{"x": 1}] = 1
# this fails and raises a ValueError
# ValueError: Variable 'b': indexer {'x': 1} not available
Relevant log output
Traceback (most recent call last):
File "xarray/core/dataset.py", line 1591, in _setitem_check
var_k = var[key]
File "xarray/core/dataarray.py", line 740, in __getitem__
return self.isel(indexers=self._item_key_to_dict(key))
File "xarray/core/dataarray.py", line 1204, in isel
variable = self._variable.isel(indexers, missing_dims=missing_dims)
File "xarray/core/variable.py", line 1181, in isel
indexers = drop_dims_from_indexers(indexers, self.dims, missing_dims)
File "xarray/core/utils.py", line 834, in drop_dims_from_indexers
raise ValueError(
ValueError: Dimensions {'x'} do not exist. Expected one or more of ('y',)
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "xarray/core/dataset.py", line 1521, in __setitem__
value = self._setitem_check(key, value)
File "xarray/core/dataset.py", line 1593, in _setitem_check
raise ValueError(
ValueError: Variable 'b': indexer {'x': 1} not available
Anything else we need to know?
No response
Environment
INSTALLED VERSIONS
commit: None
python: 3.9.1 (default, Jan 13 2021, 15:21:08)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-44)]
python-bits: 64
OS: Linux
OS-release: 3.10.0-1160.49.1.el7.x86_64
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8
LOCALE: ('en_US', 'UTF-8')
libhdf5: 1.12.0
libnetcdf: 4.7.4
xarray: 0.21.1
pandas: 1.4.0
numpy: 1.21.5
scipy: 1.7.3
netCDF4: 1.5.8
pydap: None
h5netcdf: None
h5py: None
Nio: None
zarr: None
cftime: 1.5.1.1
nc_time_axis: None
PseudoNetCDF: None
rasterio: None
cfgrib: None
iris: None
bottleneck: None
dask: None
distributed: None
matplotlib: 3.5.1
cartopy: None
seaborn: None
numbagg: None
fsspec: None
cupy: None
pint: None
sparse: None
setuptools: 49.2.1
pip: 22.0.3
conda: None
pytest: 6.2.5
IPython: 8.0.0
sphinx: None
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with _setitem_check in xarray/core/dataset.py and reproduce the minimal example showing assignment through a selector-dict. Compare this path with selection and the related DataArray, Variable, and utils entry points in the traceback. Done means assignment succeeds when a selected dimension is absent from another variable, without raising the reported ValueError.
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
- 45/100