Skip to content

Commit 3e6278f

Browse files
maxrjonesTomNicholaspre-commit-ci[bot]
authored
Allow concatenating empty variables (#641)
* Allow concatenating empty variables * Add test * add test for indexing scalar with an ellipsis * simplify test * release note * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * add note about test being a regression test --------- Co-authored-by: Tom Nicholas <[email protected]> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent ebed400 commit 3e6278f

File tree

4 files changed

+18
-2
lines changed

4 files changed

+18
-2
lines changed

docs/releases.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@
4747
By [Tom Nicholas](https://github.com/TomNicholas).
4848
- Fixed bug causing coordinates to be demoted to data variables when writing to Icechunk ([#574](https://github.com/zarr-developers/VirtualiZarr/issues/574), [#588](https://github.com/zarr-developers/VirtualiZarr/pull/588))
4949
By [Tom Nicholas](https://github.com/TomNicholas).
50+
- Fixed bug when indexing a scalar ManifestArray with an ellipsis([#596](https://github.com/zarr-developers/VirtualiZarr/issues/596), [#641](https://github.com/zarr-developers/VirtualiZarr/pull/641))
51+
By [Max Jones](https://github.com/maxrjones) and [Tom Nicholas](https://github.com/TomNicholas).
5052

5153
### Documentation
5254

virtualizarr/manifests/array.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ def __getitem__(
259259

260260
# check value is valid
261261
indexer = _possibly_expand_trailing_ellipsis(indexer, self.ndim)
262-
if len(indexer) != self.ndim:
262+
if len(indexer) != self.ndim and self.ndim > 0:
263263
raise ValueError(
264264
f"Invalid indexer for array. Indexer length must be less than or equal to the number of dimensions in the array, "
265265
f"but indexer={indexer} has length {len(indexer)} and array has {self.ndim} dimensions."
@@ -362,7 +362,7 @@ def _possibly_expand_trailing_ellipsis(
362362
"""
363363
final_dim_indexer = indexer[-1]
364364
if final_dim_indexer == ...:
365-
if len(indexer) > ndim:
365+
if len(indexer) > ndim and ndim > 0:
366366
raise ValueError(
367367
f"Invalid indexer for array. Indexer length must be less than or equal to the number of dimensions in the array, "
368368
f"but indexer={indexer} has length {len(indexer)} and array has {ndim} dimensions."

virtualizarr/tests/test_manifests/test_array.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,11 @@ def test_index_with_numpy_array_notimplemented(self, manifest_array, dodgy_index
422422
with pytest.raises(NotImplementedError):
423423
marr[dodgy_indexer]
424424

425+
def test_indexing_scalar_with_ellipsis(self, manifest_array):
426+
# regression test for https://github.com/zarr-developers/VirtualiZarr/pull/641
427+
marr = manifest_array(shape=(), chunks=())
428+
assert marr[...] == marr
429+
425430

426431
def test_to_xarray(array_v3_metadata):
427432
chunks = (5, 10)

virtualizarr/tests/test_xarray.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -878,3 +878,12 @@ def test_drop_variables(netcdf4_file):
878878
file_url=netcdf4_file, object_store=store, parser=parser, drop_variables=["air"]
879879
) as vds:
880880
assert "air" not in vds.variables
881+
882+
883+
def test_concat_zero_dimensional_var(manifest_array):
884+
# regression test for https://github.com/zarr-developers/VirtualiZarr/pull/641
885+
marr = manifest_array(shape=(), chunks=())
886+
vds1 = xr.Dataset({"a": marr})
887+
vds2 = xr.Dataset({"a": marr})
888+
result = xr.concat([vds1, vds2], dim="time", coords="minimal", compat="override")
889+
assert result["a"].sizes == {"time": 2}

0 commit comments

Comments
 (0)