Skip to content

Support indexing when it removes a dimension of length 1#879

Draft
aaron-kaplan wants to merge 2 commits intozarr-developers:mainfrom
iridl:squeeze
Draft

Support indexing when it removes a dimension of length 1#879
aaron-kaplan wants to merge 2 commits intozarr-developers:mainfrom
iridl:squeeze

Conversation

@aaron-kaplan
Copy link
Copy Markdown

@aaron-kaplan aaron-kaplan commented Feb 20, 2026

This is to support xr.DataArray.squeeze. It implements the following comment that was in manifests/indexing.py:

# TODO cover possibility of indexing into a length-1 dimension (which just removes that dimension)?

I'll raise a couple points as in-line comments.

todo:

  • Convert the removed coordinate to a scalar attribute
  • Closes #xxxx
  • Tests added
  • Tests passing
  • Full type hint coverage
  • Changes are documented in docs/releases.rst
  • New functions/methods are listed in api.rst
  • New functionality has documentation

@codecov
Copy link
Copy Markdown

codecov Bot commented Feb 20, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 89.05%. Comparing base (ba1f464) to head (57a1042).

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #879      +/-   ##
==========================================
- Coverage   89.11%   89.05%   -0.06%     
==========================================
  Files          34       34              
  Lines        1956     1965       +9     
==========================================
+ Hits         1743     1750       +7     
- Misses        213      215       +2     
Files with missing lines Coverage Δ
virtualizarr/manifests/indexing.py 94.59% <100.00%> (+1.73%) ⬆️

... and 1 file with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

from zarr.core.metadata.v3 import ArrayV3Metadata

from virtualizarr.manifests.array_api import expand_dims
from .manifest import ChunkManifest
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Sibling imports avoid loading the parent package's init.py, which sometimes resolves circular import problems. As I write this I can't remember if this was actually necessary in this case. I'll check.

Copy link
Copy Markdown
Member

@TomNicholas TomNicholas Mar 10, 2026

Choose a reason for hiding this comment

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

It shouldn't be. If needed I think you can just import ChunkManifest inside array_selection_1d instead.


def apply_selection_1d(
marr: "ManifestArray", indexer_1d: T_BasicIndexer_1d, length: int
marr: "ManifestArray", indexer_1d: T_BasicIndexer_1d, axis: int, length: int
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

This function signature wasn't dictated by xarray, was it? Seems like it's not implementable without the axis parameter.

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.

No, these are all implementation details, as xarray only cares about the signatures of certain methods on the ManifestArray class (for indexing __getitem__ is what matters). So feel free to alter the the functions in this file as necessary. (That is one reason I put the indexing internals in this separate indexing.py file.)

lengths=np.squeeze(marr.manifest._lengths, axis=axis),
validate_paths=False
)
marr = marr.__class__(metadata, manifest)
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

I would rather say ManifestArray instead of marr.__class__ for clarity, but importing array.py led to a circular import. Seems to me that this function should really be a method of ManifestArray, not split out into a separate file.

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.

The more conventional fix is to just import ManifestArray inside the function that needs it.

@TomNicholas
Copy link
Copy Markdown
Member

Thanks for working on this @aaron-kaplan ! I'm planning a release soon - are you likely to continue working on this or would you rather I picked it up?

@aaron-kaplan
Copy link
Copy Markdown
Author

@TomNicholas I hoped to get back to it eventually, but I'm not actively working on it right now. If you can do it sooner that would be great.

I only addressed the "squeeze" case, but it seems like it other types of indexing should be straightforward to handle as well, as long as they slice along chunk boundaries.

@TomNicholas
Copy link
Copy Markdown
Member

If you can do it sooner that would be great.

No problem - I'll probably need to implement this feature for myself soon.

it seems like it other types of indexing should be straightforward to handle as well, as long as they slice along chunk boundaries.

Yes precisely - see #51.

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.

2 participants