You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Behaviour from Dataset.broadcast_like is strange and inconsistent with how arithmetic ops on Datasets actually broadcast #10031
This looks like a bug to me, but if the behaviour is intentional can we please document the reason and draw attention to it with big alarm bells in the docs, it as it's very unexpected and can lead to undesired blow-ups in the size of arrays.
Either way can we please have a version of the broadcast API which broadcasts datasets in the same way that arithmetic operations broadcast them?
Minimal example — the example is as focused as reasonably possible to demonstrate the underlying issue in xarray.
Complete example — the example is self-contained, including all data and the text of any traceback.
Verifiable example — the example copy & pastes into an IPython prompt or Binder notebook, returning the result.
New issue — a search of GitHub Issues suggests this is not a duplicate.
Recent environment — the issue occurs with the latest version of xarray and its dependencies.
Relevant log output
Anything else we need to know?
This is related to #6549, which has been open as a feature request for ~3 years, although not quite the same. Opening a bug anyway with an more focused / minimal illustration of why this makes very little sense.
Broadcasting in the way you request is a no-op in Xarray-land, so you don't need broadcast_like.
If two Xarray objects have dimensions of different names (your example), they are automatically broadcastable against each other. Nothing needs to be done.
If they share a dimension (which means said dimensions has the same name), then that dimension is checked for alignment. Use xr.align with its join option to do the checking / alignment you need.
Apologies, now I get it after re-reading #6549 a few times. IIUC you'd like the behaviour of broadcast_like to broadcast like variables against each other. Shall we close in favor of #6549?
Broadcasting in the way you request is a no-op in Xarray-land, so you don't need broadcast_like.
So the example above was just a minimal reproducer to illustrate the non-sensical / inconsistent behaviour. In practise we run into related issues when broadcasting different datasets in less trivial cases.
Also -- you say "Broadcasting in the way you request is a no-op" -- I agree it should be a no-op, but the above clearly illustrates that it isn't, which is kind of the point here right?
The other ticket is about broadcasting Dataset against DataArray, I think it's likely the same underlying cause, but if I had to summarize the overall problem, it's that behaviour of xarray.broadcast (and Dataset.broadcast_like etc) is not consistent with how actual arithmetic operations broadcast, in cases where Datasets are involved.
No worries. Is it intentional that xr.broadcast / broadcast_like is inconsistent with how arithmetic ops actually broadcast? If not I'd suggest making #6549 a bug not a FR.
Is it intentional that xr.broadcast / broadcast_like is inconsistent with how arithmetic ops actually broadcast?
broadcast is quite old so presumably its intentional. IIUC I added broadcast_like as a handy alias to broadcast so it inherited that behaviour (unintentionally). Seems to me that _like implies stricter behaviour by default.
If it is intentional it would be good to confirm, and to document what the behaviour is with Datasets. I rather suspect it's behaviour that most people wouldn't want or expect, although I may be missing the original motivation.
Activity
dcherian commentedon Feb 6, 2025
Broadcasting in the way you request is a no-op in Xarray-land, so you don't need
broadcast_like.xr.alignwith itsjoinoption to do the checking / alignment you need.What we don't support yet, is allowing broadcast to insert a size-1 unlabeled dimension of the same name. This I have found useful in combination with
apply_ufunc(https://github.com/xarray-contrib/flox/blob/ca576812e78b3978421eace6e9dde5a76729ebcc/flox/xarray.py#L45-L62) so that you can avoid useless extra work in a downstream function that takes unlabeled arrays. This would require #2171 also.Admittedly, our documentation on this should be a lot better. See https://tutorial.xarray.dev/fundamentals/02.3_aligning_data_objects.html.
cc @headtr1ck
dcherian commentedon Feb 6, 2025
Apologies, now I get it after re-reading #6549 a few times. IIUC you'd like the behaviour of
broadcast_liketo broadcast like variables against each other. Shall we close in favor of #6549?mjwillson commentedon Feb 6, 2025
So the example above was just a minimal reproducer to illustrate the non-sensical / inconsistent behaviour. In practise we run into related issues when broadcasting different datasets in less trivial cases.
Also -- you say "Broadcasting in the way you request is a no-op" -- I agree it should be a no-op, but the above clearly illustrates that it isn't, which is kind of the point here right?
The other ticket is about broadcasting Dataset against DataArray, I think it's likely the same underlying cause, but if I had to summarize the overall problem, it's that behaviour of
xarray.broadcast(andDataset.broadcast_likeetc) is not consistent with how actual arithmetic operations broadcast, in cases where Datasets are involved.dcherian commentedon Feb 6, 2025
Thanks for the clarification. Yes i misunderstood your initial post. Apologies for that. I'm closing in favor of #6549.
mjwillson commentedon Feb 6, 2025
No worries. Is it intentional that
xr.broadcast/broadcast_likeis inconsistent with how arithmetic ops actually broadcast? If not I'd suggest making #6549 a bug not a FR.dcherian commentedon Feb 6, 2025
broadcastis quite old so presumably its intentional. IIUC I addedbroadcast_likeas a handy alias to broadcast so it inherited that behaviour (unintentionally). Seems to me that_likeimplies stricter behaviour by default.mjwillson commentedon Feb 6, 2025
If it is intentional it would be good to confirm, and to document what the behaviour is with Datasets. I rather suspect it's behaviour that most people wouldn't want or expect, although I may be missing the original motivation.