Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Zarr: drop "source" and "original_shape" from encoding #7500

Merged
merged 4 commits into from
Feb 7, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ Bug fixes
By `Kai Mühlbauer <https://github.com/kmuehlbauer>`_ and `Scott Chamberlin <https://github.com/scottcha>`_.
- Handle ``keep_attrs`` option in binary operators of :py:meth:`Dataset` (:issue:`7390`, :pull:`7391`).
By `Aron Gergely <https://github.com/arongergely>`_.
- :py:func:`xarray.Dataset.to_zarr` drops now variable encodings that have been added by xarray during reading
a dataset. (:issue:`7129`, :pull:`7500`).
By `Hauke Schulz <https://github.com/observingClouds>`_.
observingClouds marked this conversation as resolved.
Show resolved Hide resolved

Documentation
~~~~~~~~~~~~~
Expand Down
5 changes: 5 additions & 0 deletions xarray/backends/zarr.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@ def extract_zarr_variable_encoding(
"""
encoding = variable.encoding.copy()

safe_to_drop = {"source", "original_shape"}
valid_encodings = {
"chunks",
"compressor",
Expand All @@ -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:
Expand Down
6 changes: 6 additions & 0 deletions xarray/tests/test_backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down