[英]Iterating over xarray. DataArray first dimension and its coordinates
Suppose I have the following DataArray
假设我有以下DataArray
arr = xarray.DataArray(np.arange(6).reshape(2,3),
dims=['A', 'B'],
coords=dict(A=['a0', 'a1'],
B=['b0', 'b1', 'b2']))
I want to iterate over the first dimension and do the following (of course I want to do something more complex than printing)我想遍历第一个维度并执行以下操作(当然我想做比打印更复杂的事情)
for coor in arr.A.values:
print(coor, arr.sel(A=coor).values)
and get并得到
a0 [0 1 2]
a1 [3 4 5]
I am new to xarray
, so I was wondering whether there was some more natural way to achieve this, something like我是xarray
的新手,所以我想知道是否有更自然的方法来实现这一点,比如
for coor, sub_arr in arr.some_method():
print(coor, sub_arr)
You can simply iterate over the DataArray - each element of the iterator will itself be a DataArray with a single value for the first coordinate:您可以简单地迭代 DataArray - 迭代器的每个元素本身都是一个 DataArray,第一个坐标只有一个值:
for a in arr:
print(a.A.values, a.values)
prints印刷
a0 [0 1 2]
a1 [3 4 5]
Note that here aAvalues
is a zero-dimensional numpy array, use the .item()
method to access the underlying scalar.注意这里的aAvalues
是一个零维的 numpy 数组,使用.item()
方法访问底层标量。
To iterate over the second dimension, you can just transpose the data:要迭代第二个维度,您只需转置数据:
for b in arr.transpose(): # or arr.T
print(b.B.values, b.values)
prints印刷
b0 [0 3]
b1 [1 4]
b2 [2 5]
For multidimensional data, you can move the dimension you want to iterate over to the first place using ellipsis:对于多维数据,您可以使用省略号将要迭代的维度移动到第一位:
for x in arr.transpose("B", ...):
...
The documentation on reshaping and reorganizing data has further details.有关重塑和重组数据的文档有更多详细信息。
It's an old question, but I find that using groupby
is cleaner and makes more intuitive sense to me than using transpose when you want to iterate some dimension other than the first:这是一个老问题,但我发现当您想迭代除第一个维度之外的某个维度时,使用groupby
比使用 transpose 更干净,对我来说更直观:
for coor, sub_arr in arr.groupby('A'):
print(coor)
print(sub_arr)
a0
<xarray.DataArray (B: 3)>
array([0, 1, 2])
Coordinates:
* B (B) <U2 'b0' 'b1' 'b2'
A <U2 'a0'
a1
<xarray.DataArray (B: 3)>
array([3, 4, 5])
Coordinates:
* B (B) <U2 'b0' 'b1' 'b2'
A <U2 'a1'
Also it seems that older versions of xarray don't handle the ellipsis correctly (see mgunyho's answer), but groupby still works correctly.此外,旧版本的 xarray 似乎无法正确处理省略号(请参阅 mgunyho 的回答),但 groupby 仍然可以正常工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.