issues: 246612712
This data as json
id | node_id | number | title | user | state | locked | assignee | milestone | comments | created_at | updated_at | closed_at | author_association | active_lock_reason | draft | pull_request | body | reactions | performed_via_github_app | state_reason | repo | type |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
246612712 | MDU6SXNzdWUyNDY2MTI3MTI= | 1497 | Best way to perform DataArray.mean while retaining coords defined in the dimension being averaged | 6200806 | closed | 0 | 2 | 2017-07-30T22:17:48Z | 2019-01-16T03:36:16Z | 2019-01-16T03:36:15Z | CONTRIBUTOR |
```python In [39]: x, y = range(2), range(3) In [40]: arr = xr.DataArray(np.random.random((2,3)), dims=['x', 'y'], coords=dict(x=x, y=y)) In [41]: coord = xr.DataArray(np.random.random((2,3)), dims=['x', 'y'], coords=dict(x=x, y=y)) In [42]: arr = arr.assign_coords(z=coord) In [43]: arr Out[43]: <xarray.DataArray (x: 2, y: 3)> array([[ 0.132368, 0.746242, 0.48783 ], [ 0.12747 , 0.751283, 0.033713]]) Coordinates: * y (y) int64 0 1 2 * x (x) int64 0 1 z (x, y) float64 0.993 0.1031 0.1808 0.2769 0.7237 0.2891 In [44]: arr.mean('x') Out[44]: <xarray.DataArray (y: 3)> array([ 0.129919, 0.748763, 0.260772]) Coordinates: * y (y) int64 0 1 2 ``` We have a use case where we'd like to preserve the coordinates. @spencerkclark came up with the following workaround, which entails converting to a dataset, promoting the coord to a variable, performing the mean, and demoting the original coord back from a variable to a coord:
This works fine, but I just feel like maybe there's an easier way to do this that we're missing. Any ideas? Thanks in advance. |
{ "url": "https://api.github.com/repos/pydata/xarray/issues/1497/reactions", "total_count": 0, "+1": 0, "-1": 0, "laugh": 0, "hooray": 0, "confused": 0, "heart": 0, "rocket": 0, "eyes": 0 } |
completed | 13221727 | issue |