I have a function that accepts multiple 2D arrays and creates two new arrays with the same shape. It was originally written to only support numpy arrays, but was "hacked" to support dask arrays if a "chunks" attribute was seen. A user who was using xarray DataArrays pointed out that this function now returns dask arrays because DataArray's have a "chunks" attribute.
I'm wondering if the dask/xarray experts can tell me what the cleanest way to support all 3 (4?) object types might be without having to duplicate code for each type (numpy array, dask array, xarray with numpy, xarray with dask). Keep in mind the inputs are 2D arrays so the masking operations involved aren't supported out of the box. The related pull request for fixing this is here. Here is what we have so far while trying to avoid adding xarray and dask as required dependencies:
if hasattr(az_, 'chunks') and not hasattr(az_, 'loc'):
# dask array, but not xarray
import dask.array as da
az_ = da.where(top_s > 0, az_ + np.pi, az_)
az_ = da.where(az_ < 0, az_ + 2 * np.pi, az_)
elif hasattr(az_, 'loc'):
# xarray
az_.data[top_s > 0] += np.pi
az_.data[az_.data < 0] += 2 * np.pi
else:
az_[top_s > 0] += np.pi
az_[az_ < 0] += 2 * np.pi
Edit: Is there an attribute that is semi-unique to xarray objects?