【问题标题】:How do I use xarray groupby_bins to group by a time array?如何使用 xarray groupby_bins 按时间数组分组?
【发布时间】:2017-05-19 04:11:50
【问题描述】:

我有一个多维数据对象,它有一个时间轴。我需要根据常规时间序列(例如每小时或每天)对数据进行分箱(以便随后计算每个时间箱内的相关性并获得相关时间序列)。但是,当我尝试使用groupby_bins 时,我得到TypeError: Cannot cast ufunc less input from dtype('<m8[ns]') to dtype('<m8') with casting rule 'same_kind'

# xr is xarray; pd is pandas
In [109]: C = numpy.random.randint(-2000, 2000, dtype='int16', size=(5000, 56, 20))

In [110]: D = xr.DataArray(C, dims=("time", "scanpos", "channel"), coords={"time": pd.date_range("2000-01-01T00:00:00", periods=5000, freq='1min')})

In [111]: D.groupby_bins("time", pd.date_range(*D["time"].data[[0,-1]], freq="1H"))
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-111-7e7cda1ad060> in <module>()
----> 1 D.groupby_bins("time", pd.date_range(*D["time"].data[[0,-1]], freq="1H"))

/dev/shm/gerrit/venv/stable-3.5/lib/python3.5/site-packages/xarray/core/common.py in groupby_bins(self, group, bins, right, labels, precision, include_lowest, squeeze)
    397                                 cut_kwargs={'right': right, 'labels': labels,
    398                                             'precision': precision,
--> 399                                             'include_lowest': include_lowest})
    400 
    401     def rolling(self, min_periods=None, center=False, **windows):

/dev/shm/gerrit/venv/stable-3.5/lib/python3.5/site-packages/xarray/core/groupby.py in __init__(self, obj, group, squeeze, grouper, bins, cut_kwargs)
    190             raise TypeError("Can't specify both `grouper` and `bins`.")
    191         if bins is not None:
--> 192             binned = pd.cut(group.values, bins, **cut_kwargs)
    193             new_dim_name = group.name + '_bins'
    194             group = DataArray(binned, group.coords, name=new_dim_name)

/dev/shm/gerrit/venv/stable-3.5/lib/python3.5/site-packages/pandas/tools/tile.py in cut(x, bins, right, labels, retbins, precision, include_lowest)
    112     else:
    113         bins = np.asarray(bins)
--> 114         if (np.diff(bins) < 0).any():
    115             raise ValueError('bins must increase monotonically.')
    116 

TypeError: Cannot cast ufunc less input from dtype('<m8[ns]') to dtype('<m8') with casting rule 'same_kind'

如何在xarrays groupby_bins 中使用时间轴?我尝试使用匹配 dtypes 的时间轴,但将 dtype 传递给 pd.date_range 似乎没有效果,即使 dtypes 相同(不知道为什么它们不在这个玩具示例中,但这是一个不同的问题)错误仍然存​​在。


附:我也对完全绕过pd.date_range 的解决方案感到满意。

【问题讨论】:

    标签: python arrays numpy time-series python-xarray


    【解决方案1】:

    groupby_bins 用于数字数据,尽管没有内在的理由不应该适用于日期(这确实有点令人困惑)。解决合并日期问题的最简单方法是使用resample method

    D.resample("time", "1H")
    

    【讨论】:

    • 这给了我每个时间段的平均值;我需要完整的数据来计算一个相关矩阵,它增加而不是减少维数,所以它不是一个减少操作,所以我希望我可能无法直接使用resample,即使使用我自己的how-function;它与尺寸混淆。我可以用一个明确的 for 循环来做到这一点。
    • 啊。我们还没有这种能力,尽管感兴趣的一方可以相对简单地添加它。请参阅github.com/pydata/xarray/issues/364 进行讨论。
    猜你喜欢
    • 2021-05-15
    • 2020-06-01
    • 2016-08-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多