Working with numpy-like arrays

Warning

This feature should be considered experimental. Please report any bug you may find on xarray’s github repository.

Numpy-like arrays (duck array) extend the numpy.ndarray with additional features, like propagating physical units or a different layout in memory.

DataArray and Dataset objects can wrap these duck arrays, as long as they satisfy certain conditions (see Integrating with duck arrays).

Note

For dask support see Parallel computing with Dask.

Missing features

Most of the API does support duck array objects, but there are a few areas where the code will still cast to numpy arrays:

  • dimension coordinates, and thus all indexing operations:

    • Dataset.sel() and DataArray.sel()

    • Dataset.loc() and DataArray.loc()

    • Dataset.drop_sel() and DataArray.drop_sel()

    • Dataset.reindex(), Dataset.reindex_like(), DataArray.reindex() and DataArray.reindex_like(): duck arrays in data variables and non-dimension coordinates won’t be casted

  • functions and methods that depend on external libraries or features of numpy not covered by __array_function__ / __array_ufunc__:

    • Dataset.ffill() and DataArray.ffill() (uses bottleneck)

    • Dataset.bfill() and DataArray.bfill() (uses bottleneck)

    • Dataset.interp(), Dataset.interp_like(), DataArray.interp() and DataArray.interp_like() (uses scipy): duck arrays in data variables and non-dimension coordinates will be casted in addition to not supporting duck arrays in dimension coordinates

    • Dataset.rolling_exp() and DataArray.rolling_exp() (uses numbagg)

    • Dataset.rolling() and DataArray.rolling() (uses internal functions of numpy)

    • Dataset.interpolate_na() and DataArray.interpolate_na() (uses numpy.vectorize)

    • apply_ufunc() with vectorize=True (uses numpy.vectorize)

  • incompatibilities between different duck array libraries:

    • Dataset.chunk() and DataArray.chunk(): this fails if the data was not already chunked and the duck array (e.g. a pint quantity) should wrap the new dask array; changing the chunk sizes works.

Extensions using duck arrays

Here’s a list of libraries extending xarray to make working with wrapped duck arrays easier: