Python TypeError:ufunc乘法不能使用dtype(“

Python TypeError:ufunc乘法不能使用dtype(“,python,datetime,python-xarray,Python,Datetime,Python Xarray,我目前有一份从1996年7月18日到2006年12月31日的每小时读数的netCDF文件,希望计算数据变量的JJA季节平均值。我已尝试遵循上所示的示例: 但是,我遇到了以下错误: TypeError Traceback (most recent call last) <ipython-input-45-51dd727eba52> in <module> 6 7 # Calculate

我目前有一份从1996年7月18日到2006年12月31日的每小时读数的netCDF文件,希望计算数据变量的JJA季节平均值。我已尝试遵循上所示的示例:

但是,我遇到了以下错误:

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

问题似乎在于将datetime变量乘以浮点。调用ds*权重时,权重将乘以ds中的每个变量。我猜ds.time_bounds*权重真的没有意义,这个类型错误也同意这一点

我建议将时间界限提升到一个坐标。非索引坐标类似于数据变量,但数学运算不会影响它们。请参阅上的xarray文档

尝试ds=ds。在加权操作之前设置坐标“时间界限”

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