简体   繁体   中英

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.

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')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM