pydata / pydata/xarray

DataArray.sel methods: "inner" and "outer"

Open
#7,964 4 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement
Dominant language
Python
Stars
4.2k
Forks
1.4k
Avg merge
2d 15h
Merged PRs (30d)
14

Description

Is your feature request related to a problem?

Currently, in the case of inexact matches, you can select the nearest coordinate, the next coordinate, or the previous coordinate. This works well for single matches, but not for slices. If I want to extract the ROI that definitely contains all the data from 5.5 to 10.5, I don't want 5 to 10, or 6 to 11: I want 5 to 11 (or possibly 6 to 10).

It would be helpful to be able to treat the start and stop boundary differently when it comes to inexact matches for selection.

Describe the solution you'd like

The addition of methods="inner" and methods="outer" to the DataArray.sel method (names up for debate). When paired with slice selections, "inner" would take the next-right of the left boundary and the next-left of the right boundary; "outer" would take the next-left of the left boundary and next-right of the right boundary.

This wouldn't be compatible with scalar or vector indexing. These could either default to None or "nearest". It might also be nice to treat different dimensions differently, but that would be a separate feature, and probably can already be achieved using successive calls to .sel.

"inner" is currently the default behaviour for indexing with a slice (for monotonic increasing coordinates). This seems like it could be extended for non-monotonic coordinates and for decreasing coordinates (reversing the dimension depending on the stride of the slice).

Describe alternatives you've considered

Something like numpy.logical_and(coords >= left, coords =< right) and logical indexing for inner, logical_noting the inverse for outer. This isn't very efficient for large arrays.

Additional context

I have 3D images and need to get an ROI around a particular point. I want to make sure that the extracted array is at least as large as a certain padding around the point. I could just add the resolution to the padding but this seems like it would be useful in other contexts.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

The entry point is DataArray.sel; begin by tracing its slice-selection behavior and how inexact matching is handled. Define and test inner and outer semantics for slice boundaries, including increasing, decreasing, and non-monotonic coordinates, while keeping scalar and vector indexing excluded.

Written by the indexing model from the issue text.

Assessment

Tech stack
numpy, python
Domain
data
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
28/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.