Skip to content

DataArray.groupby.apply with a generic ndarray function #326

Description

@IamJeffG

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.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions