我有一个具有一个时间轴的多维数据对象。我需要根据常规时间序列(例如每小时或每天)对数据进行分箱(以便随后计算每个时间箱内的相关性并获得相关时间序列)。但是,当我尝试使用时,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'
如何将时间轴与xarray
s一起使用groupby_bins
?我尝试使用匹配 dtypes 的时间轴,但传递dtype
topd.date_range
似乎没有效果,即使 dtypes 相同(不知道为什么它们不在这个玩具示例中,但这是一个不同的问题),错误仍然存在。
PS我也对pd.date_range
完全绕过的解决方案感到满意。