微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

xarray:遍历数据集的所有坐标

如何解决xarray:遍历数据集的所有坐标

我要遍历nd xarray数据集中的所有坐标,请考虑:

Dimensions:  (x0: 2,x1: 4,x2: 3)
Coordinates:
  * x0       (x0) int64 0 1
  * x1       (x1) int64 0 1 2 3
  * x2       (x2) int64 0 1 2
Data variables:
    y0       (x0,x1,x2) ...

我的用例是,我将读取每个坐标元组,将其发送给函数,然后将结果保存在数据集中。我当前的代码

# first build a list of tuples containing every dimension-coordinate pair
# we add the dimension so we can find this location later on
locations = []
for coord in dataset.coords:
    dim = []
    for v in dataset[coord].values
        dim.append((coord,v))
    locations.append(dim)
# locations Now looks like [('x0',0),('x0,1),('x1',0) ... ('x2',2)]

# cross product the locations to create all possible coordinates
for tup in itertools.product(*locations):
    # tup looks like (('x0',3),('x2',2)) etc.
    val = my_function(marshal(tup))     # call the function with the relevant values
    dataset['y0'].loc[dict(tup)] = val  # set the returned value to the right position in the dataset

这很好用,但我想知道是否还有更简洁的方法来实现相同目的?

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。