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/issues/3941#issuecomment-610142267,https://api.github.com/repos/pydata/xarray/issues/3941,610142267,MDEyOklzc3VlQ29tbWVudDYxMDE0MjI2Nw==,30388627,2020-04-07T02:45:26Z,2020-04-07T02:45:26Z,NONE,"@dcherian Sorry for the misunderstanding. I tried again for the 3d array, it works well ;)
```
import xarray as xr
import numpy as np
x = 2
y = 4
z = 3
data = np.arange(x*y*z).reshape(z, x, y)
# input array
a = xr.DataArray(data, dims=['z', 'y', 'x'])
# start_index array
sindex = xr.DataArray(np.full_like(a[0, ...], 0), dims=['y', 'x'])
# end_index array
eindex = xr.DataArray(np.full_like(a[0, ...], 1), dims=['y', 'x'])
zindex = a.z.copy(data=np.arange(a.sizes[""z""]))
sub_z = (zindex >= sindex) & (zindex <= eindex)
sum_a = a.where(sub_z).sum('z', keepdims=True)
print(a)
print(sum_a)
```
```
array([[[ 0, 1, 2, 3],
[ 4, 5, 6, 7]],
[[ 8, 9, 10, 11],
[12, 13, 14, 15]],
[[16, 17, 18, 19],
[20, 21, 22, 23]]])
Dimensions without coordinates: z, y, x
array([[[ 8., 10., 12., 14.],
[16., 18., 20., 22.]]])
Dimensions without coordinates: z, y, x
```","{""total_count"": 0, ""+1"": 0, ""-1"": 0, ""laugh"": 0, ""hooray"": 0, ""confused"": 0, ""heart"": 0, ""rocket"": 0, ""eyes"": 0}",,594900245
https://github.com/pydata/xarray/issues/3941#issuecomment-609882875,https://api.github.com/repos/pydata/xarray/issues/3941,609882875,MDEyOklzc3VlQ29tbWVudDYwOTg4Mjg3NQ==,2448579,2020-04-06T15:59:37Z,2020-04-06T15:59:37Z,MEMBER,why not?,"{""total_count"": 0, ""+1"": 0, ""-1"": 0, ""laugh"": 0, ""hooray"": 0, ""confused"": 0, ""heart"": 0, ""rocket"": 0, ""eyes"": 0}",,594900245
https://github.com/pydata/xarray/issues/3941#issuecomment-609850841,https://api.github.com/repos/pydata/xarray/issues/3941,609850841,MDEyOklzc3VlQ29tbWVudDYwOTg1MDg0MQ==,30388627,2020-04-06T15:04:23Z,2020-04-06T15:04:23Z,NONE,"@dcherian Excellent solution!
If we upgrade this to 3d array and sum by `z` axis, it seems that method isn't suitable:
```
import xarray as xr
import numpy as np
x = 2
y = 2
z = 3
data = np.arange(x*y*z).reshape(z, y, x)
# input array
a = xr.DataArray(data, dims=['z', 'y', 'x'])
# start_index array
sindex = xr.DataArray(np.full_like(a[0, ...], 0), dims=['y', 'x'])
# end_index array
eindex = xr.DataArray(np.full_like(a[0, ...], 1), dims=['y', 'x'])
```","{""total_count"": 0, ""+1"": 0, ""-1"": 0, ""laugh"": 0, ""hooray"": 0, ""confused"": 0, ""heart"": 0, ""rocket"": 0, ""eyes"": 0}",,594900245
https://github.com/pydata/xarray/issues/3941#issuecomment-609827384,https://api.github.com/repos/pydata/xarray/issues/3941,609827384,MDEyOklzc3VlQ29tbWVudDYwOTgyNzM4NA==,2448579,2020-04-06T14:24:53Z,2020-04-06T14:24:53Z,MEMBER,"`where` is usually the solution for this kind of problem. I added the `keepdims` to keep the `y` dimension after the sum.
``` python
yindex = a.y.copy(data=np.arange(a.sizes[""y""])) # generate DataArray of indexes
a.where((yindex >= sindex) & (yindex <= eindex)).sum(""y"", keepdims=True)
```
Please close if this answers your question.","{""total_count"": 4, ""+1"": 4, ""-1"": 0, ""laugh"": 0, ""hooray"": 0, ""confused"": 0, ""heart"": 0, ""rocket"": 0, ""eyes"": 0}",,594900245
https://github.com/pydata/xarray/issues/3941#issuecomment-609728948,https://api.github.com/repos/pydata/xarray/issues/3941,609728948,MDEyOklzc3VlQ29tbWVudDYwOTcyODk0OA==,30388627,2020-04-06T11:09:10Z,2020-04-06T11:09:10Z,NONE,"**Solution (Boolean)**
```
# stack indexes
index_list = np.column_stack((sindex, eindex))
# all false array
boolean_array = np.zeros(a.shape, dtype=bool)
# iterate and assign true
for row in range(len(index_list)):
boolean_array[row, np.arange(index_list[row][0], index_list[row][1]+1)] = True
sum_a = a.where(boolean_array).sum(dim='y')
```","{""total_count"": 0, ""+1"": 0, ""-1"": 0, ""laugh"": 0, ""hooray"": 0, ""confused"": 0, ""heart"": 0, ""rocket"": 0, ""eyes"": 0}",,594900245