Skip to content

Commit df4a4b1

Browse files
author
Joe Hamman
authored
Fix for zarr encoding bug (#2320)
* simple fix for zarr encoding bug * remove test asserting error raised for bad chunk sizes * bump for ci
1 parent 4df048c commit df4a4b1

File tree

3 files changed

+21
-9
lines changed

3 files changed

+21
-9
lines changed

doc/whats-new.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,10 @@ Bug fixes
6161
attribute being set.
6262
(:issue:`2201`)
6363
By `Thomas Voigt <https://github.com/tv3141>`_.
64+
- Fixed a bug in ``zarr`` backend which prevented use with datasets with
65+
invalid chunk size encoding after reading from an existing store
66+
(:issue:`2278`).
67+
By `Joe Hamman <https://github.com/jhamman>`_.
6468

6569
- Tests can be run in parallel with pytest-xdist
6670
By `Tony Tung <https://github.com/ttung>`_.

xarray/backends/zarr.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,8 @@ def _determine_zarr_chunks(enc_chunks, var_chunks, ndim):
102102
enc_chunks_tuple = tuple(enc_chunks)
103103

104104
if len(enc_chunks_tuple) != ndim:
105-
raise ValueError("zarr chunks tuple %r must have same length as "
106-
"variable.ndim %g" %
107-
(enc_chunks_tuple, ndim))
105+
# throw away encoding chunks, start over
106+
return _determine_zarr_chunks(None, var_chunks, ndim)
108107

109108
for x in enc_chunks_tuple:
110109
if not isinstance(x, int):

xarray/tests/test_backends.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1404,12 +1404,6 @@ def test_chunk_encoding_with_dask(self):
14041404
with self.roundtrip(ds_chunk4) as actual:
14051405
self.assertEqual((4,), actual['var1'].encoding['chunks'])
14061406

1407-
# specify incompatible encoding
1408-
ds_chunk4['var1'].encoding.update({'chunks': (5, 5)})
1409-
with pytest.raises(ValueError) as e_info:
1410-
with self.roundtrip(ds_chunk4) as actual:
1411-
pass
1412-
assert e_info.match('chunks')
14131407

14141408
# TODO: remove this failure once syncronized overlapping writes are
14151409
# supported by xarray
@@ -1522,6 +1516,21 @@ def test_to_zarr_compute_false_roundtrip(self):
15221516
with self.open(store) as actual:
15231517
assert_identical(original, actual)
15241518

1519+
def test_encoding_chunksizes(self):
1520+
# regression test for GH2278
1521+
# see also test_encoding_chunksizes_unlimited
1522+
nx, ny, nt = 4, 4, 5
1523+
original = xr.Dataset({}, coords={'x': np.arange(nx),
1524+
'y': np.arange(ny),
1525+
't': np.arange(nt)})
1526+
original['v'] = xr.Variable(('x', 'y', 't'), np.zeros((nx, ny, nt)))
1527+
original = original.chunk({'t': 1, 'x': 2, 'y': 2})
1528+
1529+
with self.roundtrip(original) as ds1:
1530+
assert_equal(ds1, original)
1531+
with self.roundtrip(ds1.isel(t=0)) as ds2:
1532+
assert_equal(ds2, original.isel(t=0))
1533+
15251534

15261535
@requires_zarr
15271536
class ZarrDictStoreTest(BaseZarrTest, TestCase):

0 commit comments

Comments
 (0)