Skip to content

fix(indexing): drop stale chunksizes encoding when dims change (#11028) - #11928

Open
aniruddhaadak80 wants to merge 3 commits into
pydata:mainfrom
aniruddhaadak80:fix/chunksizes-encoding-isel
Open

aniruddhaadak80 wants to merge 3 commits into
pydata:mainfrom
aniruddhaadak80:fix/chunksizes-encoding-isel

Conversation

@aniruddhaadak80

Copy link
Copy Markdown
Contributor

Description

Closes #11028.

encoding["chunksizes"] is a positional tuple aligned with a variable's dimensions. When isel() removes a dimension (e.g. selects a single index), the variable's dims change but the encoding is copied unchanged, leaving a stale N-dimensional chunksizes on an N-1 dimensional variable:

import numpy as np
import xarray as xr

ds = xr.Dataset({"images": (("time", "y", "x"), np.zeros((5, 10, 20)))})
ds["images"].encoding = {"chunksizes": (1, 10, 20)}
selected = ds.isel(time=2)

selected.images.shape          # (10, 20)
selected.images.encoding       # {'chunksizes': (1, 10, 20)}  <- stale

Writers then have to detect and discard the mismatched value, so a user's chunking preference is silently lost.

The fix drops encoding["chunksizes"] in Variable._finalize_indexing_result and IndexVariable._finalize_indexing_result when the resulting dims differ from the source dims. Other encoding entries (e.g. _FillValue) are untouched, and chunksizes is preserved for indexing that keeps the same dimensions, such as isel(time=slice(0, 2)). No public API change.

This is a targeted step toward the broader goal expressed in the issue discussion of removing encoding after the first operation; it deliberately only removes the one entry that is invalid after a dim change.

A doc/whats-new.rst entry was added under the unreleased "Bug Fixes" section.

Checklist

Tests

New test in xarray/tests/test_dataset.py covering both the dimension-dropping case (stale chunksizes removed, other encoding kept) and the dimension-preserving slice (chunksizes kept).

Actual results in a worktree-local environment (Python 3.12, NumPy 2.5.3, pandas 3.0.6, Windows). A local sitecustomize.py on PYTHONPATH gave the tagless editable dask checkout a parseable __version__; nothing from it is in the diff.

$ python -m pytest -o addopts="" xarray/tests/test_dataset.py -k stale_chunksizes -q
  with fix:                       1 passed
  with xarray/core/variable.py reverted: 1 failed, 587 deselected

$ python -m pytest -o addopts="" xarray/tests/test_dataset.py \
    xarray/tests/test_variable.py xarray/tests/test_dataarray.py -q
  with fix:       22 failed, 1765 passed, 135 skipped, 13 xfailed, 5 xpassed
  without fix:    22 failed, 1764 passed, 135 skipped, 13 xfailed, 5 xpassed

The only difference is the one added test. The 22 failures are pre-existing in this environment: pandas 3.0.6 refuses to import its optional xarray integration because the tagless editable install reports xarray 0.0.0. They are categorical / extension-array / from_dataframe tests and fail identically without the fix.

$ python -m ruff check xarray/core/variable.py xarray/tests/test_dataset.py
All checks passed!
$ python -m ruff format --check xarray/core/variable.py xarray/tests/test_dataset.py
2 files already formatted

Limitations

  • Only chunksizes is dropped; other dimension-dependent encoding keys, if any, are out of scope.
  • Backends that already validate chunksizes on write (e.g. netCDF4) keep doing so.
  • Verified on Python 3.12 / NumPy 2.5.3 / pandas 3.0.6 / Windows only. The full suite and docs build were not run on this host.

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: OpenCode (AI coding agent)

An AI coding agent located the encoding copy in the indexing finalizers, drafted the fix and the regression test, and ran the verification above. I reviewed the diff and each claimed result.

Copilot AI balanced review requested due to automatic review settings November 22, 2026 17:17

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing.

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

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

encoding['chunksizes'] can become stale after isel() removes a dimension

2 participants