TypeError:ufunc乘法不能使用类型为dtype('<M8 [ns]')和dtype('float64')的操作数

时间:2020-05-13 15:50:49

标签: python datetime python-xarray

我目前有一个netCDF文件,该文件的小时读数为1996年7月18日至2006年12月31日,并希望计算数据变量的JJA季节性平均值。我尝试遵循http://xarray.pydata.org/en/latest/examples/monthly-means.html上显示的示例:

ds = xr.open_dataset('BE_Vie.H.nc')
ds

>> <xarray.Dataset>
>> Dimensions:      (nt: 2, time: 91632, x: 1, y: 1)
>> Coordinates:
>>   * time         (time) datetime64[ns] 1996-07-18T01:00:00 ... 2006-12-31
>>     latitude     (y, x) float32 ...
>>     longitude    (y, x) float32 ...
>> Dimensions without coordinates: nt, x, y
>> Data variables:
>>     time_bounds  (time, nt) datetime64[ns] ...
>>     gpp_gb       (time, y, x) float32 ...
>>     resp_p_gb    (time, y, x) float32 ...
>>     resp_s_gb    (time, y, x) float32 ...
>>     ftl_gb       (time, y, x) float32 ...
>>     latent_heat  (time, y, x) float32 ...
>>     rad_net      (time, y, x) float32 ...
>>     sw_down      (time, y, x) float32 ...
>>     precip       (time, y, x) float32 ...
>>     t1p5m_gb     (time, y, x) float32 ...
>>     q1p5m_gb     (time, y, x) float32 ...

month_length = ds.time.dt.days_in_month
month_length

>> <xarray.DataArray 'days_in_month' (time: 91632)>
>> array([31, 31, 31, ..., 31, 31, 31])
>> Coordinates:
>>   * time     (time) datetime64[ns] 1996-07-18T01:00:00 ... 2006-12-31

# Calculate the weights by grouping by 'time.season'.
weights = month_length.groupby('time.season') / month_length.groupby('time.season').sum()

# Test that the sum of the weights for each season is 1.0
np.testing.assert_allclose(weights.groupby('time.season').sum().values, np.ones(4))

# Calculate the weighted average
ds_weighted = (ds * weights).groupby('time.season').sum(dim='time')

但是,我遇到此错误:

TypeError                                 Traceback (most recent call last)
<ipython-input-45-51dd727eba52> in <module>
      6 
      7 # Calculate the weighted average
----> 8 ds_weighted = (ds * weights).groupby('time.season').sum(dim='time')

/anaconda3/lib/python3.7/site-packages/xarray/core/dataset.py in func(self, other)
   4774                 self, other = align(self, other, join=align_type, copy=False)
   4775             g = f if not reflexive else lambda x, y: f(y, x)
-> 4776             ds = self._calculate_binary_op(g, other, join=align_type)
   4777             return ds
   4778 

/anaconda3/lib/python3.7/site-packages/xarray/core/dataset.py in _calculate_binary_op(self, f, other, join, inplace)
   4845         else:
   4846             other_variable = getattr(other, "variable", other)
-> 4847             new_vars = {k: f(self.variables[k], other_variable) for k in self.data_vars}
   4848         ds._variables.update(new_vars)
   4849         ds._dims = calculate_dimensions(ds._variables)

/anaconda3/lib/python3.7/site-packages/xarray/core/dataset.py in <dictcomp>(.0)
   4845         else:
   4846             other_variable = getattr(other, "variable", other)
-> 4847             new_vars = {k: f(self.variables[k], other_variable) for k in self.data_vars}
   4848         ds._variables.update(new_vars)
   4849         ds._dims = calculate_dimensions(ds._variables)

/anaconda3/lib/python3.7/site-packages/xarray/core/variable.py in func(self, other)
   1987                 new_data = (
   1988                     f(self_data, other_data)
-> 1989                     if not reflexive
   1990                     else f(other_data, self_data)
   1991                 )

TypeError: ufunc multiply cannot use operands with types dtype('<M8[ns]') and dtype('float64')

我应该如何转换我的时间变量以使其工作?在这个问题上的任何帮助将不胜感激-谢谢!!

有关时间变量的更多详细信息:

ds.time

>> <xarray.DataArray 'time' (time: 91632)>
>> array(['1996-07-18T01:00:00.000000000', '1996-07-18T02:00:00.000000000',
>>        '1996-07-18T03:00:00.000000000', ..., '2006-12-30T22:00:00.000000000',
>>        '2006-12-30T23:00:16.000000000', '2006-12-31T00:00:00.000000000'],
>>       dtype='datetime64[ns]')
>> Coordinates:
>>   * time     (time) datetime64[ns] 1996-07-18T01:00:00 ... 2006-12-31
>> Attributes:
>>     standard_name:  time
>>     long_name:      Time of data
>>     bounds:         time_bounds

1 个答案:

答案 0 :(得分:1)

问题似乎出在日期时间变量乘以浮点数。调用(ds * weights)时,权重将乘以ds中的每个变量。我猜想ds.time_bounds * weights真的没有道理,而且这个TypeError也同意。

我建议将time_bounds提升为坐标。非索引坐标类似于数据变量,但数学运算不会影响它们。请参阅Coordinates上的xarray文档。

在加权操作之前尝试ds = ds.set_coords('time_bounds')