diff --git a/doc/whats-new.rst b/doc/whats-new.rst index 26bd72b0727..458765e9d70 100644 --- a/doc/whats-new.rst +++ b/doc/whats-new.rst @@ -55,6 +55,9 @@ Bug fixes By `Kai Mühlbauer `_ and `Scott Chamberlin `_. - Handle ``keep_attrs`` option in binary operators of :py:meth:`Dataset` (:issue:`7390`, :pull:`7391`). By `Aron Gergely `_. +- :py:func:`xarray.Dataset.to_zarr` now drops variable encodings that have been added by xarray during reading + a dataset. (:issue:`7129`, :pull:`7500`). + By `Hauke Schulz `_. Documentation ~~~~~~~~~~~~~ diff --git a/xarray/backends/zarr.py b/xarray/backends/zarr.py index e30e7e9f4d8..ae400c2b340 100644 --- a/xarray/backends/zarr.py +++ b/xarray/backends/zarr.py @@ -230,6 +230,7 @@ def extract_zarr_variable_encoding( """ encoding = variable.encoding.copy() + safe_to_drop = {"source", "original_shape"} valid_encodings = { "chunks", "compressor", @@ -238,6 +239,10 @@ def extract_zarr_variable_encoding( "write_empty_chunks", } + for k in safe_to_drop: + if k in encoding: + del encoding[k] + if raise_on_invalid: invalid = [k for k in encoding if k not in valid_encodings] if invalid: diff --git a/xarray/tests/test_backends.py b/xarray/tests/test_backends.py index b49500bd00c..c0dace53976 100644 --- a/xarray/tests/test_backends.py +++ b/xarray/tests/test_backends.py @@ -2039,6 +2039,12 @@ def test_chunk_encoding_with_dask(self) -> None: # don't actually check equality because the data could be corrupted pass + def test_drop_encoding(self): + ds = open_example_dataset("example_1.nc") + encodings = {v: {**ds[v].encoding} for v in ds.data_vars} + with self.create_zarr_target() as store: + ds.to_zarr(store, encoding=encodings) + def test_hidden_zarr_keys(self) -> None: expected = create_test_data() with self.create_store() as store: