Description
For operations where arrays are aggregated but then combined, the keepdims=True
option for NumPy aggregations is convenient.
We should consider supporting this in xarray as well. Aggregating a DataArray/Dataset with keepdims=True
(or maybe keep_dims=True
) would remove all original coordinates along aggregated dimensions and return a result with a dimension of size 1 without any coordinates, e.g.,
>>> array = xr.DataArray([1, 2, 3], dims='x', coords={'x': ['a', 'b', 'c']})
>>> array.mean(keepdims=True)
<xarray.DataArray (x: 1)>
array([2.])
Dimensions without coordinates: x
In case, array.mean(keepdims=True()
is equivalent to array.mean().expand_dims('x')
but in general this equivalent does not hold, because the location of the original dimension is lost.
Implementation-wise, we have two options:
- Pass on
keepdims=True
to NumPy functions likenumpy.mean()
, or - Implement
keepdims=True
ourselves, inVariable.reduce()
.
I think I like option 2 a little better, because it places fewer requirements on aggregation functions. For example, functions like bottleneck.nanmean()
don't accept a keepdims
argument.