了解xarray groupby

时间:2016-06-28 00:13:03

标签: python python-xarray

我正在尝试计算每组中的成员数量,类似于pandas.DataFrame.groupby.count。但是,它似乎并没有起作用。这是一个例子:

In [1]: xr_test = xr.DataArray(np.random.rand(6), coords=[[10,10,11,12,12,12]], dims=['dim0'])
        xr_test
Out[1]: <xarray.DataArray (dim0: 6)>
        array([ 0.92908804,  0.15495709,  0.85304435,  0.24039265,  0.3755476 ,
                0.29261274])
        Coordinates:
          * dim0     (dim0) int32 10 10 11 12 12 12

In [2]: xr_test.groupby('dim0').count()
Out[2]: <xarray.DataArray (dim0: 6)>
        array([1, 1, 1, 1, 1, 1])
        Coordinates:
          * dim0     (dim0) int32 10 10 11 12 12 12

但是,我希望这个输出:

Out[2]: <xarray.DataArray (dim0: 3)>
        array([2, 1, 3])
        Coordinates:
          * dim0     (dim0) int32 10 11 12

发生了什么?

换句话说:

In [3]: xr_test.to_series().groupby(level=0).count()
Out[3]: dim0
        10    2
        11    1
        12    3
        dtype: int64

1 个答案:

答案 0 :(得分:2)

这是一个错误! Xarray目前假设(在这种情况下是错误的)假设对应于维度的坐标具有所有唯一值。这通常是一个好主意,但不应该被要求。如果你做另一个坐标,这应该可以正常工作,例如, === Begin generated DRL === package com.sample; //generated from Decision Table import com.sample.Screens; // rule values at C11, header at C6 rule "Screens_11" when var:Screens(deliveryChannel == "Y") usePointsAppl == "Y" contactDetails == "Y" riskScore == "Y" reasonCode == 100 currentScreen == 303 then var.setNextScreen(306); System.out.println(var.getNextScreen()); end // rule values at C12, header at C6 rule "Screens_12" when var:Screens(deliveryChannel == "Y") usePointsAppl == "Y" contactDetails == "Y" riskScore == "Y" reasonCode == 100 currentScreen == 303 then var.setNextScreen(306); System.out.println(var.getNextScreen()); end === End generated DRL ===