Add a drop_outer option to .where()

Open
#11,883 1 comment 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
48/100
Issue type
Feature
Clarity
Mostly clear
Activity status
Active
Tech stack
python
Domain
data

Research direction

Start by reading the implementation and tests for .where() to understand how drop=True currently handles coordinate labels. Then assess how an outer-only trim could preserve internal NaN gaps and coordinate spacing; the issue does not name specific files or tests. Done means a drop_outer option trims only outer all-NaN regions while retaining internal gaps.

Written by the indexing model from the issue text.

Description

enhancement
Is your feature request related to a problem?

Currently, .where() supports drop=True, which removes coordinate labels where all values are NaN. However, this also removes internal rows or columns of NaN values, which can disrupt the structure of regularly spaced arrays.

Describe the solution you'd like

It would be useful to have an additional option, perhaps drop_outer=True, which only removes outer coordinate labels where all values are NaN, effectively trimming the array to the smallest bounding region containing valid data while preserving internal gaps and coordinate spacing.

Describe alternatives you've considered

This is relatively easy to achieve using .isel(dim=slice(...)) after calling .where() with drop=False, but it would be very helpful to have this functionality built into .where().

Additional context

No response

Dominant language
Python
Stars
4.2k
Forks
1.5k
Avg merge
2d 21h
Merged PRs (30d)
74

Getting set up

Open in Codespaces

Starts the project's dev container in your browser, under your own GitHub account.

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.

More from pydata/xarray

All issues in pydata/xarray

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.