Closed
Description
Need to apply a transformation function across one dimension of a DataArray, where that non-xray function speaks in ndarrays. Currently the only ways to do this involve wrapping the function. An example:
import numpy as np
import xray
from scipy.ndimage.morphology import binary_opening
da = xray.DataArray(np.random.random_integers(0, 1, (10, 10, 3)),
dims=['row', 'col', 'time'])
# I want to apply an operation the 2D image at each point in time
da.groupby('time').apply(binary_opening)
# AttributeError: 'numpy.ndarray' object has no attribute 'dims'
def wrap_binary_opening(da, **kwargs):
return xray.DataArray(binary_opening(da.values, **kwargs), da.coords)
da.groupby('time').apply(wrap_binary_opening)
da.groupby('time').apply(wrap_binary_opening, iterations=2) # func may take custom args
My proposed solution is that apply
would automatically coerce func
's return value to a DataArray.