You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In a test, we have two datasets, which are essentially the same, but one has two time values and the other has one time value.
When loaded with xr.open_zarr, selecting the values at a single time point works with the dataset that has two time values, but raises the following error for the dataset with one time value:
numcodecs/blosc.pyx:365: in numcodecs.blosc.decompress
error: ValueError: buffer source array is read-only
This is the full traceback:
.venv/lib/python3.10/site-packages/xarray/core/dataarray.py:811: in values
return self.variable.values
.venv/lib/python3.10/site-packages/xarray/core/variable.py:554: in values
return _as_array_or_item(self._data)
.venv/lib/python3.10/site-packages/xarray/core/variable.py:352: in _as_array_or_item
data = np.asarray(data)
.venv/lib/python3.10/site-packages/dask/array/core.py:1700: in __array__
x = self.compute()
.venv/lib/python3.10/site-packages/dask/base.py:375: in compute
(result,) = compute(self, traverse=False, **kwargs)
.venv/lib/python3.10/site-packages/dask/base.py:661: in compute
results = schedule(dsk, keys, **kwargs)
.venv/lib/python3.10/site-packages/xarray/core/indexing.py:580: in __array__
return np.asarray(self.get_duck_array(), dtype=dtype)
.venv/lib/python3.10/site-packages/xarray/core/indexing.py:583: in get_duck_array
return self.array.get_duck_array()
.venv/lib/python3.10/site-packages/xarray/core/indexing.py:794: in get_duck_array
return self.array.get_duck_array()
.venv/lib/python3.10/site-packages/xarray/core/indexing.py:657: in get_duck_array
array = self.array[self.key]
.venv/lib/python3.10/site-packages/xarray/backends/zarr.py:166: in __getitem__
return indexing.explicit_indexing_adapter(
.venv/lib/python3.10/site-packages/xarray/core/indexing.py:1018: in explicit_indexing_adapter
result = raw_indexing_method(raw_key.tuple)
.venv/lib/python3.10/site-packages/xarray/backends/zarr.py:156: in _getitem
return self._array[key]
.venv/lib/python3.10/site-packages/zarr/core.py:800: in __getitem__
result = self.get_basic_selection(pure_selection, fields=fields)
.venv/lib/python3.10/site-packages/zarr/core.py:926: in get_basic_selection
return self._get_basic_selection_nd(selection=selection, out=out, fields=fields)
.venv/lib/python3.10/site-packages/zarr/core.py:968: in _get_basic_selection_nd
return self._get_selection(indexer=indexer, out=out, fields=fields)
.venv/lib/python3.10/site-packages/zarr/core.py:1343: in _get_selection
self._chunk_getitems(
.venv/lib/python3.10/site-packages/zarr/core.py:2181: in _chunk_getitems
self._process_chunk(
.venv/lib/python3.10/site-packages/zarr/core.py:2049: in _process_chunk
self._compressor.decode(cdata, dest)
numcodecs/blosc.pyx:564: in numcodecs.blosc.Blosc.decode
???
numcodecs/blosc.pyx:365: in numcodecs.blosc.decompress
???
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
> ???
E ValueError: buffer source array is read-only
What did you expect to happen?
I expected a single value to be returned in both cases.
Note: If the value passed to sel is a list containing a single time value, then the error doesn't occur.
Minimal Complete Verifiable Example
frompathlibimportPathimporttempfileimportnumpyasnpimportpandasaspdimportxarrayasxrtimes1=pd.date_range("2019-01-01", periods=1, freq="YS")
times2=pd.date_range("2019-01-01", periods=2, freq="YS")
ds1=xr.Dataset({"abc": ("time", [1])}, coords={"time": ("time", times1)})
ds2=xr.Dataset({"abc": ("time", [1, 2])}, coords={"time": ("time", times2)})
# save to zarr and reloadtemp1=Path(tempfile.TemporaryDirectory().name) /"temp1.zarr"temp2=Path(tempfile.TemporaryDirectory().name) /"temp2.zarr"ds1.to_zarr(temp1)
ds2.to_zarr(temp2)
ds1_reloaded=xr.open_zarr(temp1)
ds2_reloaded=xr.open_zarr(temp2)
# everything is fine with ds2_reloadedassertds2_reloaded.sel(time="2019-01-01").abc.values==np.array(1)
assertds2_reloaded.sel(time=["2019-01-01"]).abc.values==np.array([1])
# this works for ds1_reloaded:assertds1_reloaded.sel(time=["2019-01-01"]).abc.values==np.array([1])
# this raises a value error:ds1_reloaded.sel(time="2019-01-01").abc.values# this raises the same error:ds1_reloaded.sel(time="2019-01-01").as_numpy()
# but this worksds1_reloaded.as_numpy()
MVCE confirmation
Minimal example — the example is as focused as reasonably possible to demonstrate the underlying issue in xarray.
Complete example — the example is self-contained, including all data and the text of any traceback.
Verifiable example — the example copy & pastes into an IPython prompt or Binder notebook, returning the result.
New issue — a search of GitHub Issues suggests this is not a duplicate.
Recent environment — the issue occurs with the latest version of xarray and its dependencies.
Relevant log output
# traceback for ValueError from .sel followed by .as_numpy:ValueErrorTraceback (mostrecentcalllast)
CellIn[58], line1---->1ds1_reloaded.sel(time="2019-01-01").as_numpy()
File~/Documents/openghg/.venv/lib/python3.10/site-packages/xarray/core/dataset.py:1434, inDataset.as_numpy(self)
1425defas_numpy(self) ->Self:
1426""" 1427 Coerces wrapped data and coordinates into numpy arrays, returning a Dataset. 1428 (...) 1432 DataArray.to_numpy : Returns only the data as a numpy.ndarray object. 1433 """->1434numpy_variables= {k: v.as_numpy() fork, vinself.variables.items()}
1435returnself._replace(variables=numpy_variables)
File~/Documents/openghg/.venv/lib/python3.10/site-packages/xarray/core/dataset.py:1434, in<dictcomp>(.0)
1425defas_numpy(self) ->Self:
1426""" 1427 Coerces wrapped data and coordinates into numpy arrays, returning a Dataset. 1428 (...) 1432 DataArray.to_numpy : Returns only the data as a numpy.ndarray object. 1433 """->1434numpy_variables= {k: v.as_numpy() fork, vinself.variables.items()}
1435returnself._replace(variables=numpy_variables)
File~/Documents/openghg/.venv/lib/python3.10/site-packages/xarray/namedarray/core.py:861, inNamedArray.as_numpy(self)
859defas_numpy(self) ->Self:
860"""Coerces wrapped data into a numpy array, returning a Variable."""-->861returnself._replace(data=self.to_numpy())
File~/Documents/openghg/.venv/lib/python3.10/site-packages/xarray/namedarray/core.py:857, inNamedArray.to_numpy(self)
855"""Coerces wrapped data to numpy and returns a numpy.ndarray"""856# TODO an entrypoint so array libraries can choose coercion method?-->857returnto_numpy(self._data)
File~/Documents/openghg/.venv/lib/python3.10/site-packages/xarray/namedarray/pycompat.py:111, into_numpy(data, **kwargs)
109ifis_chunked_array(data):
110chunkmanager=get_chunked_array_type(data)
-->111data, *_=chunkmanager.compute(data, **kwargs)
112ifisinstance(data, array_type("cupy")):
113data=data.get()
File~/Documents/openghg/.venv/lib/python3.10/site-packages/xarray/namedarray/daskmanager.py:86, inDaskManager.compute(self, *data, **kwargs)
81defcompute(
82self, *data: Any, **kwargs: Any83 ) ->tuple[np.ndarray[Any, _DType_co], ...]:
84fromdask.arrayimportcompute--->86returncompute(*data, **kwargs)
File~/Documents/openghg/.venv/lib/python3.10/site-packages/dask/base.py:661, incompute(traverse, optimize_graph, scheduler, get, *args, **kwargs)
658postcomputes.append(x.__dask_postcompute__())
660withshorten_traceback():
-->661results=schedule(dsk, keys, **kwargs)
663returnrepack([f(r, *a) forr, (f, a) inzip(results, postcomputes)])
File~/Documents/openghg/.venv/lib/python3.10/site-packages/xarray/core/indexing.py:580, inImplicitToExplicitIndexingAdapter.__array__(self, dtype, copy)
578returnnp.asarray(self.get_duck_array(), dtype=dtype, copy=copy)
579else:
-->580returnnp.asarray(self.get_duck_array(), dtype=dtype)
File~/Documents/openghg/.venv/lib/python3.10/site-packages/xarray/core/indexing.py:583, inImplicitToExplicitIndexingAdapter.get_duck_array(self)
582defget_duck_array(self):
-->583returnself.array.get_duck_array()
File~/Documents/openghg/.venv/lib/python3.10/site-packages/xarray/core/indexing.py:794, inCopyOnWriteArray.get_duck_array(self)
793defget_duck_array(self):
-->794returnself.array.get_duck_array()
File~/Documents/openghg/.venv/lib/python3.10/site-packages/xarray/core/indexing.py:657, inLazilyIndexedArray.get_duck_array(self)
653array=apply_indexer(self.array, self.key)
654else:
655# If the array is not an ExplicitlyIndexedNDArrayMixin,656# it may wrap a BackendArray so use its __getitem__-->657array=self.array[self.key]
659# self.array[self.key] is now a numpy array when660# self.array is a BackendArray subclass661# and self.key is BasicIndexer((slice(None, None, None),))662# so we need the explicit check for ExplicitlyIndexed663ifisinstance(array, ExplicitlyIndexed):
File~/Documents/openghg/.venv/lib/python3.10/site-packages/xarray/backends/zarr.py:166, inZarrArrayWrapper.__getitem__(self, key)
164elifisinstance(key, indexing.OuterIndexer):
165method=self._oindex-->166returnindexing.explicit_indexing_adapter(
167key, array.shape, indexing.IndexingSupport.VECTORIZED, method168 )
File~/Documents/openghg/.venv/lib/python3.10/site-packages/xarray/core/indexing.py:1018, inexplicit_indexing_adapter(key, shape, indexing_support, raw_indexing_method)
996"""Support explicit indexing by delegating to a raw indexing method. 997 998 Outer and/or vectorized indexers are supported by indexing a second time (...) 1015 Indexing result, in the form of a duck numpy-array. 1016 """1017raw_key, numpy_indices=decompose_indexer(key, shape, indexing_support)
->1018result=raw_indexing_method(raw_key.tuple)
1019ifnumpy_indices.tuple:
1020# index the loaded np.ndarray1021indexable=NumpyIndexingAdapter(result)
File~/Documents/openghg/.venv/lib/python3.10/site-packages/xarray/backends/zarr.py:156, inZarrArrayWrapper._getitem(self, key)
155def_getitem(self, key):
-->156returnself._array[key]
File~/Documents/openghg/.venv/lib/python3.10/site-packages/zarr/core.py:800, inArray.__getitem__(self, selection)
798result=self.get_orthogonal_selection(pure_selection, fields=fields)
799else:
-->800result=self.get_basic_selection(pure_selection, fields=fields)
801returnresultFile~/Documents/openghg/.venv/lib/python3.10/site-packages/zarr/core.py:926, inArray.get_basic_selection(self, selection, out, fields)
924returnself._get_basic_selection_zd(selection=selection, out=out, fields=fields)
925else:
-->926returnself._get_basic_selection_nd(selection=selection, out=out, fields=fields)
File~/Documents/openghg/.venv/lib/python3.10/site-packages/zarr/core.py:968, inArray._get_basic_selection_nd(self, selection, out, fields)
962def_get_basic_selection_nd(self, selection, out=None, fields=None):
963# implementation of basic selection for array with at least one dimension964965# setup indexer966indexer=BasicIndexer(selection, self)
-->968returnself._get_selection(indexer=indexer, out=out, fields=fields)
File~/Documents/openghg/.venv/lib/python3.10/site-packages/zarr/core.py:1343, inArray._get_selection(self, indexer, out, fields)
1340ifmath.prod(out_shape) >0:
1341# allow storage to get multiple items at once1342lchunk_coords, lchunk_selection, lout_selection=zip(*indexer)
->1343self._chunk_getitems(
1344lchunk_coords,
1345lchunk_selection,
1346out,
1347lout_selection,
1348drop_axes=indexer.drop_axes,
1349fields=fields,
1350 )
1351ifout.shape:
1352returnoutFile~/Documents/openghg/.venv/lib/python3.10/site-packages/zarr/core.py:2181, inArray._chunk_getitems(self, lchunk_coords, lchunk_selection, out, lout_selection, drop_axes, fields)
2179forckey, chunk_select, out_selectinzip(ckeys, lchunk_selection, lout_selection):
2180ifckeyincdatas:
->2181self._process_chunk(
2182out,
2183cdatas[ckey],
2184chunk_select,
2185drop_axes,
2186out_is_ndarray,
2187fields,
2188out_select,
2189partial_read_decode=partial_read_decode,
2190 )
2191else:
2192# check exception type2193ifself._fill_valueisnotNone:
File~/Documents/openghg/.venv/lib/python3.10/site-packages/zarr/core.py:2049, inArray._process_chunk(self, out, cdata, chunk_selection, drop_axes, out_is_ndarray, fields, out_selection, partial_read_decode)
2047ifisinstance(cdata, PartialReadBuffer):
2048cdata=cdata.read_full()
->2049self._compressor.decode(cdata, dest)
2050else:
2051ifisinstance(cdata, UncompressedPartialReadBufferV3):
Filenumcodecs/blosc.pyx:564, innumcodecs.blosc.Blosc.decode()
Filenumcodecs/blosc.pyx:365, innumcodecs.blosc.decompress()
Filenumcodecs/compat_ext.pyx:16, innumcodecs.compat_ext.Buffer.__cinit__()
ValueError: buffersourcearrayisread-only
Anything else we need to know?
No response
Environment
INSTALLED VERSIONS
------------------
commit: None
python: 3.10.14 (main, May 9 2024, 10:37:23) [Clang 15.0.0 (clang-1500.3.9.4)]
python-bits: 64
OS: Darwin
OS-release: 23.6.0
machine: arm64
processor: arm
byteorder: little
LC_ALL: None
LANG: en_GB.UTF-8
LOCALE: ('en_GB', 'UTF-8')
libhdf5: 1.12.2
libnetcdf: 4.9.3-development
What happened?
In a test, we have two datasets, which are essentially the same, but one has two time values and the other has one time value.
When loaded with
xr.open_zarr
, selecting the values at a single time point works with the dataset that has two time values, but raises the following error for the dataset with one time value:numcodecs/blosc.pyx:365: in numcodecs.blosc.decompress
error: ValueError: buffer source array is read-only
This is the full traceback:
What did you expect to happen?
I expected a single value to be returned in both cases.
Note: If the value passed to
sel
is a list containing a single time value, then the error doesn't occur.Minimal Complete Verifiable Example
MVCE confirmation
Relevant log output
Anything else we need to know?
No response
Environment
xarray: 2024.10.0
pandas: 2.2.2
numpy: 1.26.4
scipy: 1.13.0
netCDF4: 1.6.5
pydap: None
h5netcdf: 1.3.0
h5py: 3.11.0
zarr: 2.18.0
cftime: 1.6.3
nc_time_axis: 1.4.1
iris: None
bottleneck: None
dask: 2024.5.0
distributed: None
matplotlib: 3.8.4
cartopy: None
seaborn: None
numbagg: None
fsspec: 2024.3.1
cupy: None
pint: None
sparse: None
flox: None
numpy_groupies: None
setuptools: 69.5.1
pip: 24.0
conda: None
pytest: 8.2.0
mypy: 1.7.1
IPython: 8.24.0
sphinx: 7.4.7
The text was updated successfully, but these errors were encountered: