简体   繁体   English

沿 xarray 中的单个维度对多个坐标进行分组

[英]groupby multiple coords along a single dimension in xarray

I have an xarray with multiple coordinates along a single dimension.我有一个沿单个维度具有多个坐标的 xarray。 In the example below, coords a and b are defined along dimension dim1 .在下面的示例中,坐标ab沿维度dim1定义。 How would I groupby using two coordinates that are defined along the same dimension(s)?我将如何使用沿相同维度定义的两个坐标进行groupby Unlike this question , I am not trying to group along different dimensions, but a single one.这个问题不同,我不是要按照不同的维度进行分组,而是要对一个维度进行分组。

import xarray as xr

d = xr.DataArray([[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]],
    coords={
        'a': ('dim1',['A', 'A', 'B', 'B']),
        'b': ('dim1',['1', '2', '1', '2']),
        'c': ('dim2',['x', 'y', 'z'])
    },
    dims=['dim1', 'dim2'])
d.groupby(['a','b']) # this gives: TypeError: `group` must be an xarray.DataArray or the name of an xarray variable or dimension

This is my current workaround:这是我目前的解决方法:

import numpy as np
import xarray as xr

def groupby_multicoords(da, fields):
    common_dim = da.coords[fields[0]].dims[0]
    tups_arr = np.empty(len(da[common_dim]), dtype=object)
    tups_arr[:] = list(zip(*(da[f].values for f in fields)))
    return da.assign_coords(grouping_zip=xr.DataArray(tups_arr, dims=common_dim)).groupby('grouping_zip')

and then, groupby_multicoords(da=d, fields=['a', 'b'])然后, groupby_multicoords(da=d, fields=['a', 'b'])

However, after grouping I am still left with the 'grouping_zip' coord.但是,在分组之后,我仍然保留了“grouping_zip”坐标。 I would be grateful to replace it with d.groupby(['a','b']) ..我将不胜感激用d.groupby(['a','b'])替换它..

您可以使用.stack(new=[“dim1”,”dim2”)它们堆叠到单个 MultiIndex 中,然后按该维度.stack(new=[“dim1”,”dim2”)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM