Ken T
Ken T

Reputation: 2553

How to check if xarray's `DataArray` is `dask.array` without loading the data into memory?

The following code can check the whether a DataArray is dask.array but it will load all data into memory and is thus very slow.

import dask.array as da
import xarray as xr

ds = xr.tutorial.open_dataset('air_temperature',
         chunks={'lat': 25, 'lon': 25, 'time': -1})

if isinstance(ds.air.data, da.Array):
    print('It is dask.Array')

I would like to get the results without loading the whole variable into memory.

Upvotes: 2

Views: 1116

Answers (1)

Michael Delgado
Michael Delgado

Reputation: 15442

xarray.DataArray.chunks will return a tuple of chunksizes if the data is a dask array, or None if not. So you can simply test with:

if ds.air.chunks is not None:
    print('It is dask.Array')

Upvotes: 4

Related Questions