xarray icon indicating copy to clipboard operation
xarray copied to clipboard

apply_ufunc and Datasets with variables without the core dimension

Open dcherian opened this issue 2 years ago • 1 comments

Is your feature request related to a problem?

Consider this example

ds = xr.Dataset({"a": ("x", [1, 2, 3]), "b": ("y", [1, 2, 3])})
xr.apply_ufunc(np.mean, ds, input_core_dims=[["x"]])

This raises

ValueError: operand to apply_ufunc has required core dimensions ['x'], but some of these dimensions are absent on an input variable: ['x']

because core dimension x is missing on variable b. This behaviour makes it annoying to use apply_ufunc on Datasets.

Describe the solution you'd like

Add a new kwarg to apply_ufunc called missing_core_dim that controls how to handle variables without all input core dimensions. This kwarg could take one of two values:

  1. "raise" - raise an error, current behaviour
  2. "copy" - skip applying the function and copy the variable from input to output.

Describe alternatives you've considered

No response

Additional context

No response

dcherian avatar Mar 21 '22 09:03 dcherian

I would like to have a "ignore" option as well, such that I can do input_core_dims=[["x","y"]] in your example.

headtr1ck avatar Oct 12 '22 16:10 headtr1ck

@shoyer what do you think of adding such a kwarg?

dcherian avatar Oct 18 '22 21:10 dcherian