When aggregating a dataset over specified dimensions I don't expect variables which don't have those dimensions to be aggregated.
What did you expect to happen?
When a weighting is applied to the aggregation, variables which do not have the aggregation dimensions are nevertheless aggregated. Presumably because the weights get broadcast across those variables. Perhaps this is the intended behavior but it seems surprising to me and should at least be documented I think.
Minimal Complete Verifiable Example
import xarray as xr
import numpy as np
var1 = np.ones((2, 2, 3))
var2 = np.ones((3))
lon = np.arange(4).reshape(2, 2)
lat = np.arange(4).reshape(2, 2)
ds = xr.Dataset(
{
"temperature": (["x", "y", "time"], var1),
"precipitation": (["time"], var2),
},
coords={
"lon": (["x", "y"], lon),
"lat": (["x", "y"], lat),
"time": np.arange(3),
},
)
print(ds.sum(['x', 'y']))
# Precipitation (with no x or y dimension) is not summed over, leading to values [1. 1. 1.]
print(ds.weighted(xr.ones_like(ds['temperature'])).sum(['x', 'y']))
# Precipitation is now summed over, leading to values [4. 4. 4.]
MVCE confirmation
[X] Minimal example — the example is as focused as reasonably possible to demonstrate the underlying issue in xarray.
[X] Complete example — the example is self-contained, including all data and the text of any traceback.
[X] Verifiable example — the example copy & pastes into an IPython prompt or Binder notebook, returning the result.
[X] New issue — a search of GitHub Issues suggests this is not a duplicate.
[X] Recent environment — the issue occurs with the latest version of xarray and its dependencies.
What happened?
When aggregating a dataset over specified dimensions I don't expect variables which don't have those dimensions to be aggregated.
What did you expect to happen?
When a weighting is applied to the aggregation, variables which do not have the aggregation dimensions are nevertheless aggregated. Presumably because the weights get broadcast across those variables. Perhaps this is the intended behavior but it seems surprising to me and should at least be documented I think.
Minimal Complete Verifiable Example
MVCE confirmation
Relevant log output
No response
Anything else we need to know?
No response
Environment