Registry Mirror
Skip to content

Conversation

@officialasishkumar

Copy link
Copy Markdown
Contributor

Description

Dataset.to_zarr and DataArray.to_zarr document -1 as a way to request a full-dimension chunk in the encoding. With zarr v3, passing that sentinel through unchanged now fails because zarr requires non-negative chunk sizes.

This normalizes -1 to the written dimension length before passing chunk metadata to zarr, preserving the documented behavior and the legacy zarr v2-compatible workflow.

Checklist

AI Disclosure

  • This PR contains AI-generated content.
    • I have tested any AI-generated content in my PR.
    • I take responsibility for any AI-generated content in my PR.
      Tools: Codex

@github-actions github-actions Bot added topic-backends topic-zarr Related to zarr storage library io labels Apr 9, 2026
@d-v-b

d-v-b commented Apr 10, 2026

Copy link
Copy Markdown
Contributor

this could be something we handle in zarr-python

@jsignell jsignell left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with @d-v-b that this is probably a convention that zarr-python should allow. The issue with doing it on the xarray side is that then when you read the file back in you have the encoding set to a particular value. You lose the original meeting "one chunk along this dim".

data["var2"].encoding.update({"chunks": (5, -1)})

with self.roundtrip(data) as actual:
assert actual["var2"].encoding["chunks"] == (5, data["var2"].shape[1])

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a bit to this test that asserts that the encoding dict on the original data object is still ({"chunks": (5, -1)}?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added this assertion in dad7145 so the test now verifies the original data object keeps encoding["chunks"] as (5, -1).

@jsignell jsignell mentioned this pull request Apr 10, 2026
5 tasks done
@hhalaby

hhalaby commented Apr 10, 2026

Copy link
Copy Markdown

@jsignell @d-v-b is removing the -1 from the chunks dictionary to avoid the error a valid temporary solution ? does the function to_zarr still behave in the same way (no chunking on dimensions not mentioned) ?

@jsignell

Copy link
Copy Markdown
Member

@jsignell @d-v-b is removing the -1 from the chunks dictionary to avoid the error a valid temporary solution ? does the function to_zarr still behave in the same way (no chunking on dimensions not mentioned) ?

Do you mean in your own code? Yeah you would get the behavior you are looking for if you explicitly set the chunks to include the full dim:

import numpy as np
import xarray as xr

zarr_negative_chunk_path = "zarrs/sample_negative_chunk.zarr"
raster_negative_chunk = xr.Dataset(
    {"foo": (("key", "y", "x"), np.zeros((2, 1, 1), dtype="i2"))},
    coords={"key": [1, 1], "y": [1], "x": [1]},
)
foo = raster_negative_chunk["foo"]
foo.encoding = {
    "dtype": "i2",
    "chunks": (1, *foo.shape[1:]),
}
raster_negative_chunk.to_zarr(zarr_negative_chunk_path)

Translate xarray's documented -1 chunk sentinel into explicit full-dimension chunk sizes before passing chunk metadata to zarr, and cover the regression with a roundtrip test.
@officialasishkumar
officialasishkumar force-pushed the fix-11288-zarr-negative-chunks branch from 8813563 to dad7145 Compare April 12, 2026 05:21

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

io topic-backends topic-zarr Related to zarr storage library

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Negative chunks break in newer version

4 participants