Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add join argument to xr.broadcast? #6304

Open
mathause opened this issue Feb 25, 2022 · 1 comment
Open

add join argument to xr.broadcast? #6304

mathause opened this issue Feb 25, 2022 · 1 comment

Comments

@mathause
Copy link
Collaborator

mathause commented Feb 25, 2022

Is your feature request related to a problem?

xr.broadcast always does an outer join:

def broadcast(*args, exclude=None):

args = align(*args, join="outer", copy=False, exclude=exclude)

This is not how the (default) broadcasting (arithmetic join) works, e.g. the following first does an inner join and then broadcasts:

import xarray as xr

da1 = xr.DataArray([[0, 1, 2]], dims=("y", "x"), coords={"x": [0, 1, 2]})
da2 = xr.DataArray([0, 1, 2, 3, 4], dims="x", coords={"x": [0, 1, 2, 3, 4]})
da1 + da2
<xarray.DataArray (y: 1, x: 3)>
array([[0, 2, 4]])
Coordinates:
  * x        (x) int64 0 1 2
Dimensions without coordinates: y

Describe the solution you'd like

Add a join argument to xr.broadcast. I would propose to leave the default as is

def broadcast(*args, exclude=None, join="outer"):
    args = align(*args, join=join, copy=False, exclude=exclude) 

Describe alternatives you've considered

  • We could make broadcast respect options -> arithmetic_join but that would be a breaking change and I am not sure how the deprecation should/ would be handled...
  • We could leave it as is.

Additional context

  • xr.broadcast should not be used often because this is should happen automatically in most cases
  • in Weighted quantile #6059 I use broadcast because I couldn't get it to work otherwise (maybe there is a better way?). However, the "outer elements" are immediately discarded again - so it's kind of pointless to do an outer join.
import numpy as np
import xarray as xr

da = xr.DataArray(np.arange(6).reshape(3, 2), coords={"dim_0": [0, 1, 2]})
w = xr.DataArray([1, 1, 1, 1, 1, 1], coords={"dim_0": [0, 1, 2, 4, 5, 6]})
da.weighted(w).quantile(0.5)
@shoyer
Copy link
Member

shoyer commented Feb 25, 2022

Adding a join argument sounds good to me. I do not remember why the default is an outer join.

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

No branches or pull requests

2 participants