html_url,issue_url,id,node_id,user,created_at,updated_at,author_association,body,reactions,performed_via_github_app,issue
https://github.com/pydata/xarray/issues/5173#issuecomment-821878676,https://api.github.com/repos/pydata/xarray/issues/5173,821878676,MDEyOklzc3VlQ29tbWVudDgyMTg3ODY3Ng==,5635139,2021-04-17T19:55:30Z,2021-04-17T19:55:30Z,MEMBER,"I'm looking at implementing this.
I think we could do this with a simple `broadcast_like` on the input, as above. But is that going to miss things that our normal machinery is capturing? Currently `.clip` is a [`unary_ops`](https://github.com/pydata/xarray/blob/v0.17.0/xarray/core/dataarray.py#L2954) which I haven't looked at in a while. And I'm not being that successful at evaluating what's going wrong from the stack trace — so posting here in case those who've worked with this recently have it cached.
One thing to note: there are different errors depending on which dimensions are missing, because of the missing broadcasting before removing the `dim` information:
```python
In [27]: da.clip(da.mean('a'))
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
in
----> 1 da.clip(da.mean('a'))
~/.asdf/installs/python/3.8.8/lib/python3.8/site-packages/xarray/core/dataarray.py in func(self, *args, **kwargs)
2964 )
2965 with np.errstate(all=""ignore""):
-> 2966 da = self.__array_wrap__(f(self.variable.data, *args, **kwargs))
2967 if keep_attrs:
2968 da.attrs = self.attrs
~/.asdf/installs/python/3.8.8/lib/python3.8/site-packages/xarray/core/ops.py in func(self, *args, **kwargs)
228 def _method_wrapper(name):
229 def func(self, *args, **kwargs):
--> 230 return _call_possibly_missing_method(self, name, args, kwargs)
231
232 func.__name__ = name
~/.asdf/installs/python/3.8.8/lib/python3.8/site-packages/xarray/core/ops.py in _call_possibly_missing_method(arg, name, args, kwargs)
214 raise
215 else:
--> 216 return method(*args, **kwargs)
217
218
~/.asdf/installs/python/3.8.8/lib/python3.8/site-packages/numpy/core/_methods.py in _clip(a, min, max, out, casting, **kwargs)
153 um.minimum, a, max, out=out, casting=casting, **kwargs)
154 elif max is None:
--> 155 return _clip_dep_invoke_with_casting(
156 um.maximum, a, min, out=out, casting=casting, **kwargs)
157 else:
~/.asdf/installs/python/3.8.8/lib/python3.8/site-packages/numpy/core/_methods.py in _clip_dep_invoke_with_casting(ufunc, out, casting, *args, **kwargs)
110 # try to deal with broken casting rules
111 try:
--> 112 return ufunc(*args, out=out, **kwargs)
113 except _exceptions._UFuncOutputCastingError as e:
114 # Numpy 1.17.0, 2019-02-24
~/.asdf/installs/python/3.8.8/lib/python3.8/site-packages/xarray/core/arithmetic.py in __array_ufunc__(self, ufunc, method, *inputs, **kwargs)
68 join = dataset_join = OPTIONS[""arithmetic_join""]
69
---> 70 return apply_ufunc(
71 ufunc,
72 *inputs,
~/.asdf/installs/python/3.8.8/lib/python3.8/site-packages/xarray/core/computation.py in apply_ufunc(func, input_core_dims, output_core_dims, exclude_dims, vectorize, join, dataset_join, dataset_fill_value, keep_attrs, kwargs, dask, output_dtypes, output_sizes, meta, dask_gufunc_kwargs, *args)
1126 # feed DataArray apply_variable_ufunc through apply_dataarray_vfunc
1127 elif any(isinstance(a, DataArray) for a in args):
-> 1128 return apply_dataarray_vfunc(
1129 variables_vfunc,
1130 *args,
~/.asdf/installs/python/3.8.8/lib/python3.8/site-packages/xarray/core/computation.py in apply_dataarray_vfunc(func, signature, join, exclude_dims, keep_attrs, *args)
269
270 data_vars = [getattr(a, ""variable"", a) for a in args]
--> 271 result_var = func(*data_vars)
272
273 if signature.num_outputs > 1:
~/.asdf/installs/python/3.8.8/lib/python3.8/site-packages/xarray/core/computation.py in apply_variable_ufunc(func, signature, exclude_dims, dask, output_dtypes, vectorize, keep_attrs, dask_gufunc_kwargs, *args)
741 data = as_compatible_data(data)
742 if data.ndim != len(dims):
--> 743 raise ValueError(
744 ""applied function returned data with unexpected ""
745 f""number of dimensions. Received {data.ndim} dimension(s) but ""
ValueError: applied function returned data with unexpected number of dimensions. Received 3 dimension(s) but expected 2 dimensions with names: ('b', 'c')
```
When the final dimension is missing:
```python
In [28]: da.clip(da.mean('c'))
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
in
----> 1 da.clip(da.mean('c'))
~/.asdf/installs/python/3.8.8/lib/python3.8/site-packages/xarray/core/dataarray.py in func(self, *args, **kwargs)
2964 )
2965 with np.errstate(all=""ignore""):
-> 2966 da = self.__array_wrap__(f(self.variable.data, *args, **kwargs))
2967 if keep_attrs:
2968 da.attrs = self.attrs
~/.asdf/installs/python/3.8.8/lib/python3.8/site-packages/xarray/core/ops.py in func(self, *args, **kwargs)
228 def _method_wrapper(name):
229 def func(self, *args, **kwargs):
--> 230 return _call_possibly_missing_method(self, name, args, kwargs)
231
232 func.__name__ = name
~/.asdf/installs/python/3.8.8/lib/python3.8/site-packages/xarray/core/ops.py in _call_possibly_missing_method(arg, name, args, kwargs)
214 raise
215 else:
--> 216 return method(*args, **kwargs)
217
218
~/.asdf/installs/python/3.8.8/lib/python3.8/site-packages/numpy/core/_methods.py in _clip(a, min, max, out, casting, **kwargs)
153 um.minimum, a, max, out=out, casting=casting, **kwargs)
154 elif max is None:
--> 155 return _clip_dep_invoke_with_casting(
156 um.maximum, a, min, out=out, casting=casting, **kwargs)
157 else:
~/.asdf/installs/python/3.8.8/lib/python3.8/site-packages/numpy/core/_methods.py in _clip_dep_invoke_with_casting(ufunc, out, casting, *args, **kwargs)
110 # try to deal with broken casting rules
111 try:
--> 112 return ufunc(*args, out=out, **kwargs)
113 except _exceptions._UFuncOutputCastingError as e:
114 # Numpy 1.17.0, 2019-02-24
~/.asdf/installs/python/3.8.8/lib/python3.8/site-packages/xarray/core/arithmetic.py in __array_ufunc__(self, ufunc, method, *inputs, **kwargs)
68 join = dataset_join = OPTIONS[""arithmetic_join""]
69
---> 70 return apply_ufunc(
71 ufunc,
72 *inputs,
~/.asdf/installs/python/3.8.8/lib/python3.8/site-packages/xarray/core/computation.py in apply_ufunc(func, input_core_dims, output_core_dims, exclude_dims, vectorize, join, dataset_join, dataset_fill_value, keep_attrs, kwargs, dask, output_dtypes, output_sizes, meta, dask_gufunc_kwargs, *args)
1126 # feed DataArray apply_variable_ufunc through apply_dataarray_vfunc
1127 elif any(isinstance(a, DataArray) for a in args):
-> 1128 return apply_dataarray_vfunc(
1129 variables_vfunc,
1130 *args,
~/.asdf/installs/python/3.8.8/lib/python3.8/site-packages/xarray/core/computation.py in apply_dataarray_vfunc(func, signature, join, exclude_dims, keep_attrs, *args)
269
270 data_vars = [getattr(a, ""variable"", a) for a in args]
--> 271 result_var = func(*data_vars)
272
273 if signature.num_outputs > 1:
~/.asdf/installs/python/3.8.8/lib/python3.8/site-packages/xarray/core/computation.py in apply_variable_ufunc(func, signature, exclude_dims, dask, output_dtypes, vectorize, keep_attrs, dask_gufunc_kwargs, *args)
722 )
723
--> 724 result_data = func(*input_data)
725
726 if signature.num_outputs == 1:
ValueError: operands could not be broadcast together with shapes (2,4,3) (2,4)
```
","{""total_count"": 0, ""+1"": 0, ""-1"": 0, ""laugh"": 0, ""hooray"": 0, ""confused"": 0, ""heart"": 0, ""rocket"": 0, ""eyes"": 0}",,860038564