home / github / issues

Menu
  • Search all tables
  • GraphQL API

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      

DataArray.mean applied along a particular dimension causes coordinates that are defined in that dimension to be dropped:

```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:

python def coord_preserving_mean(arr, *args, **kwargs): name = arr.name ds = arr.reset_coords() names = set(arr.coords) - set(arr.dims) ds = ds.mean(*args, **kwargs) return ds.set_coords(names)[name]

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.

xref https://github.com/spencerahill/aospy/issues/194

{
    "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

Links from other tables

  • 1 row from issues_id in issues_labels
  • 2 rows from issue in issue_comments
Powered by Datasette · Queries took 0.872ms · About: xarray-datasette