Skip to content

NDPointIndex should fail early and throw nice error if scipy not installed #11047

@ianhi

Description

@ianhi

What happened?

NDPointIndex has a hard dependency on scipy but when you try to use it with a default xarray install with no extras

you get this error:

➜  nd-scipy git:(main) ✗ uv run nd_scipy.py
Traceback (most recent call last):
  File "/Users/ian/Documents/dev/testing/nd-scipy/nd_scipy.py", line 19, in <module>
    ds_index = ds.set_xindex(("xx", "yy"), xr.indexes.NDPointIndex)
  File "/Users/ian/.cache/uv/environments-v2/nd-scipy-363973e27d29e760/lib/python3.14/site-packages/xarray/core/dataset.py", line 5019, in set_xindex
    index = index_cls.from_variables(coord_vars, options=options)
  File "/Users/ian/.cache/uv/environments-v2/nd-scipy-363973e27d29e760/lib/python3.14/site-packages/xarray/indexes/nd_point_index.py", line 276, in from_variables
    tree_adapter_cls(points, options=opts),
    ~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/ian/.cache/uv/environments-v2/nd-scipy-363973e27d29e760/lib/python3.14/site-packages/xarray/indexes/nd_point_index.py", line 78, in __init__
    from scipy.spatial import KDTree
ModuleNotFoundError: No module named 'scipy'

which could be confusing as a user.

What did you expect to happen?

  • An error raised on import of the Index rather only once I try to create one
  • More informative error message saying something like "to use NDPointIndex please install scipy"

Similar to how there is a nice message if you are missing a backend:

ValueError: unrecognized engine 'zarr' must be one of your download engines: ['store']. To install additional dependencies, see:
https://docs.xarray.dev/en/stable/user-guide/io.html
https://docs.xarray.dev/en/stable/getting-started-guide/installing.html

Minimal Complete Verifiable Example

# /// script
# requires-python = ">=3.14"
# dependencies = [
#     "xarray",
# ]
# ///


import numpy as np
import xarray as xr
from xarray.indexes import NDPointIndex

shape = (5, 10)
xx = xr.DataArray(np.random.uniform(0, 10, size=shape), dims=("y", "x"))
yy = xr.DataArray(np.random.uniform(0, 5, size=shape), dims=("y", "x"))
data = (xx - 5) ** 2 + (yy - 2.5) ** 2

ds = xr.Dataset(data_vars={"data": data}, coords={"xx": xx, "yy": yy})
ds_index = ds.set_xindex(("xx", "yy"), NDPointIndex)

Steps to reproduce

uv run

MVCE confirmation

  • Minimal example — the example is as focused as reasonably possible to demonstrate the underlying issue in xarray.
  • Complete example — the example is self-contained, including all data and the text of any traceback.
  • Verifiable example — the example copy & pastes into an IPython prompt or Binder notebook, returning the result.
  • New issue — a search of GitHub Issues suggests this is not a duplicate.
  • Recent environment — the issue occurs with the latest version of xarray and its dependencies.

Relevant log output

Anything else we need to know?

No response

Environment

Details

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions