home / github / issue_comments

Menu
  • Search all tables
  • GraphQL API

issue_comments: 608322689

This data as json

html_url issue_url id node_id user created_at updated_at author_association body reactions performed_via_github_app issue
https://github.com/pydata/xarray/pull/3922#issuecomment-608322689 https://api.github.com/repos/pydata/xarray/issues/3922 608322689 MDEyOklzc3VlQ29tbWVudDYwODMyMjY4OQ== 14808389 2020-04-03T09:06:42Z 2020-04-03T09:06:42Z MEMBER

The issue is that argmax produces a ndim - 1 array of indexes. In the case of 2D input data that would be 1D, so since indexing only allows to index with 1D arrays, your code works. For ndim > 2, we'd be trying to index with arrays with ndim > 1, so the indexing fails.

To get your 3D example (and potentially every N-D example) to work, simply fall back to the wrapped array's integer indexing (using coordarray.data): ```python In [2]: darray = da.from_array( ...: np.random.RandomState(0).randn(10 *20 * 30).reshape(10, 20, 30), ...: chunks=(1, 20, 30), # so we actually have multiple blocks ...: name='data_arr' ...: ) ...: array = xr.DataArray( ...: darray, ...: dims=["x", "y", 'z'], ...: coords={"x": np.arange(10), "y": np.arange(20), "z": np.arange(30)}, ...: ) ...: array Out[2]: <xarray.DataArray 'data_arr' (x: 10, y: 20, z: 30)> dask.array<data_arr, shape=(10, 20, 30), dtype=float64, chunksize=(1, 20, 30), chunktype=numpy.ndarray> Coordinates: * x (x) int64 0 1 2 3 4 5 6 7 8 9 * y (y) int64 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 * z (z) int64 0 1 2 3 4 5 6 7 8 9 10 ... 20 21 22 23 24 25 26 27 28 29

In [3]: indx = array.argmin(dim='z', keep_attrs=True, skipna=False) ...: res = indx.copy( ...: data=indx.data.map_blocks( ...: lambda ind, coord: coord[(ind,)], ...: array.z.data, ...: dtype=array.z.dtype ...: ) ...: )

In [4]: res.compute() Out[4]: <xarray.DataArray 'data_arr' (x: 10, y: 20)> array([[20, 3, 3, 11, 20, 17, 3, 27, 24, 1, 7, 4, 22, 14, 7, 18, 5, 18, 7, 19], [10, 21, 25, 3, 15, 25, 28, 8, 10, 9, 13, 3, 24, 17, 19, 23, 12, 19, 19, 28], [ 1, 26, 10, 9, 16, 8, 17, 8, 6, 24, 28, 13, 23, 22, 26, 13, 28, 11, 6, 16], [ 6, 9, 26, 27, 1, 2, 21, 8, 10, 19, 14, 14, 20, 25, 24, 4, 18, 12, 20, 2], [22, 5, 12, 17, 13, 23, 23, 8, 27, 22, 1, 19, 26, 16, 12, 17, 19, 28, 8, 12], [20, 8, 25, 13, 4, 12, 23, 13, 27, 18, 15, 28, 10, 10, 0, 12, 5, 14, 5, 27], [29, 0, 19, 7, 15, 2, 8, 8, 13, 4, 12, 1, 7, 19, 14, 0, 3, 7, 12, 9], [ 9, 8, 4, 9, 17, 6, 7, 5, 29, 0, 15, 28, 22, 6, 24, 24, 20, 0, 24, 23], [ 1, 19, 12, 20, 4, 26, 5, 13, 21, 26, 25, 10, 5, 1, 11, 21, 6, 18, 4, 21], [15, 27, 13, 7, 25, 3, 14, 14, 17, 15, 11, 4, 16, 22, 22, 23, 0, 16, 26, 13]]) Coordinates: * x (x) int64 0 1 2 3 4 5 6 7 8 9 * y (y) int64 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 `` Note that in this caseres == indxsincez = np.arange(30)`

{
    "total_count": 0,
    "+1": 0,
    "-1": 0,
    "laugh": 0,
    "hooray": 0,
    "confused": 0,
    "heart": 0,
    "rocket": 0,
    "eyes": 0
}
  591101988
Powered by Datasette · Queries took 0.771ms · About: xarray-datasette