-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
keepdims=True for xarray reductions #2170
Labels
Comments
This was referenced May 22, 2018
I second this request. The following may not be optimal, but seems to work for me as a
|
Please! It's really painful in some cases where |
Pull requests would be very welcome here. |
ScottWales
pushed a commit
to ScottWales/xarray
that referenced
this issue
Jun 19, 2019
Addresses pydata#2170 Add new option `keepdims` to xarray reduce operations, following the behaviour of Numpy. `keepdims` may be passed to reductions on either Datasets or DataArrays, and will result in the reduced dimensions being still present in the output with size 1. Coordinates that depend on the reduced dimensions will be removed from the Dataset/DataArray
3 tasks
shoyer
pushed a commit
that referenced
this issue
Jun 23, 2019
* ENH: keepdims=True for xarray reductions Addresses #2170 Add new option `keepdims` to xarray reduce operations, following the behaviour of Numpy. `keepdims` may be passed to reductions on either Datasets or DataArrays, and will result in the reduced dimensions being still present in the output with size 1. Coordinates that depend on the reduced dimensions will be removed from the Dataset/DataArray * Set the default to be `False` * Correct lint error * Apply suggestions from code review Co-Authored-By: Maximilian Roos <[email protected]> * Add test for dask and fix implementation * Move 'keepdims' up to where 'dims' is set * Fix lint, add test for scalar variable
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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 maybekeep_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.,In case,
array.mean(keepdims=True()
is equivalent toarray.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:
keepdims=True
to NumPy functions likenumpy.mean()
, orkeepdims=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 akeepdims
argument.The text was updated successfully, but these errors were encountered: