簡體   English   中英

我該如何解決這個錯誤“ufunc'減法'不能使用dtype類型的操作數('

[英]how can I solve this Error "ufunc 'subtract' cannot use operands with types dtype('<M8[ns]') and dtype('float64')"?

我必須規范化我的數據框以進行規范化的預測算法我收到此錯誤

如果我想解釋更多,我的數據框就像這種格式,尺寸為 1991 行 × 1691 列

代碼 通話時間 211323 224250
6968 2022-04-26 02:44:24.373376 61800000.0 133200000.0

我的代碼是:

column_indices = {name: i for i, name in enumerate(Code.columns)}

n = len(df1)
train_df = df1[0:int(n*0.7)]
val_df = df1[int(n*0.7):int(n*0.9)]
test_df = df1[int(n*0.9):]

num_features = df1.shape[1]
train_mean = train_df.mean()
train_std = train_df.std()

train_df = (train_df - train_mean) / train_std
val_df = (val_df - train_mean) / train_std
test_df = (test_df - train_mean) / train_std

然后我得到這個錯誤:

ufunc 'subtract' 不能使用類型為 dtype('<M8[ns]') 和 dtype('float64') 的操作數

請幫幫我。

您很可能對CallTime列中的值有疑問。 以這個導致相同錯誤的示例為例:

time_s = pd.Series([pd.Timestamp.now() + pd.Timedelta(seconds=x) for x in range(10)])

# this will raise: numpy.core._exceptions._UFuncBinaryResolutionError: ufunc 'subtract' cannot use operands with types dtype('<M8[ns]') and dtype('float64')
error_operation = time_s - 2.0  

建議:確保df1['CallTime'].dtype是日期時間格式:

time_s.dtype
dtype('<M8[ns]')

類型錯誤:ufunc 乘法不能使用類型為 dtype(' <m8[ns]') and dtype('float64')< div><div id="text_translate"><p> 我目前有一個從 1996 年 7 月 18 日到 2006 年 12 月 31 日每小時讀數的 netCDF 文件,並希望計算數據變量的 JJA 季節性平均值。 我試圖按照<a href="http://xarray.pydata.org/en/latest/examples/monthly-means.html" rel="nofollow noreferrer">http://xarray.pydata.org/en/latest/examples/monthly-means.html</a>上顯示的示例進行操作:</p><pre> ds = xr.open_dataset('BE_Vie.H.nc') ds &gt;&gt; &lt;xarray.Dataset&gt; &gt;&gt; Dimensions: (nt: 2, time: 91632, x: 1, y: 1) &gt;&gt; Coordinates: &gt;&gt; * time (time) datetime64[ns] 1996-07-18T01:00:00... 2006-12-31 &gt;&gt; latitude (y, x) float32... &gt;&gt; longitude (y, x) float32... &gt;&gt; Dimensions without coordinates: nt, x, y &gt;&gt; Data variables: &gt;&gt; time_bounds (time, nt) datetime64[ns]... &gt;&gt; gpp_gb (time, y, x) float32... &gt;&gt; resp_p_gb (time, y, x) float32... &gt;&gt; resp_s_gb (time, y, x) float32... &gt;&gt; ftl_gb (time, y, x) float32... &gt;&gt; latent_heat (time, y, x) float32... &gt;&gt; rad_net (time, y, x) float32... &gt;&gt; sw_down (time, y, x) float32... &gt;&gt; precip (time, y, x) float32... &gt;&gt; t1p5m_gb (time, y, x) float32... &gt;&gt; q1p5m_gb (time, y, x) float32... month_length = ds.time.dt.days_in_month month_length &gt;&gt; &lt;xarray.DataArray 'days_in_month' (time: 91632)&gt; &gt;&gt; array([31, 31, 31, ..., 31, 31, 31]) &gt;&gt; Coordinates: &gt;&gt; * 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')</pre><p> 但是,我遇到了這個錯誤:</p><pre> TypeError Traceback (most recent call last) &lt;ipython-input-45-51dd727eba52&gt; in &lt;module&gt; 6 7 # Calculate the weighted average ----&gt; 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) -&gt; 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) -&gt; 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 &lt;dictcomp&gt;(.0) 4845 else: 4846 other_variable = getattr(other, "variable", other) -&gt; 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) -&gt; 1989 if not reflexive 1990 else f(other_data, self_data) 1991 ) TypeError: ufunc multiply cannot use operands with types dtype('&lt;M8[ns]') and dtype('float64')</pre><p> 我應該如何轉換我的時間變量以使其工作? 對此問題的任何幫助將不勝感激 - 謝謝!</p><p> 有關時間變量的更多詳細信息:</p><pre> ds.time &gt;&gt; &lt;xarray.DataArray 'time' (time: 91632)&gt; &gt;&gt; array(['1996-07-18T01:00:00.000000000', '1996-07-18T02:00:00.000000000', &gt;&gt; '1996-07-18T03:00:00.000000000', ..., '2006-12-30T22:00:00.000000000', &gt;&gt; '2006-12-30T23:00:16.000000000', '2006-12-31T00:00:00.000000000'], &gt;&gt; dtype='datetime64[ns]') &gt;&gt; Coordinates: &gt;&gt; * time (time) datetime64[ns] 1996-07-18T01:00:00... 2006-12-31 &gt;&gt; Attributes: &gt;&gt; standard_name: time &gt;&gt; long_name: Time of data &gt;&gt; bounds: time_bounds</pre></div></m8[ns]')>

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

無法從 dtype(&#39;

[英]Cannot cast array data from dtype('<M8[ns]') to dtype('float64') according to the rule 'safe'

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

相關問題 UFuncTypeError: ufunc &#39;subtract&#39; 不能使用類型為 dtype(&#39; 類型錯誤:ufunc 減法不能使用類型為 dtype(&#39; ufunc true_divide不能使用類型為dtype(&#39;float64&#39;)和dtype(&#39; 類型錯誤:ufunc 乘法不能使用類型為 dtype(' <m8[ns]') and dtype('float64')< div><div id="text_translate"><p> 我目前有一個從 1996 年 7 月 18 日到 2006 年 12 月 31 日每小時讀數的 netCDF 文件,並希望計算數據變量的 JJA 季節性平均值。 我試圖按照<a href="http://xarray.pydata.org/en/latest/examples/monthly-means.html" rel="nofollow noreferrer">http://xarray.pydata.org/en/latest/examples/monthly-means.html</a>上顯示的示例進行操作:</p><pre> ds = xr.open_dataset('BE_Vie.H.nc') ds &gt;&gt; &lt;xarray.Dataset&gt; &gt;&gt; Dimensions: (nt: 2, time: 91632, x: 1, y: 1) &gt;&gt; Coordinates: &gt;&gt; * time (time) datetime64[ns] 1996-07-18T01:00:00... 2006-12-31 &gt;&gt; latitude (y, x) float32... &gt;&gt; longitude (y, x) float32... &gt;&gt; Dimensions without coordinates: nt, x, y &gt;&gt; Data variables: &gt;&gt; time_bounds (time, nt) datetime64[ns]... &gt;&gt; gpp_gb (time, y, x) float32... &gt;&gt; resp_p_gb (time, y, x) float32... &gt;&gt; resp_s_gb (time, y, x) float32... &gt;&gt; ftl_gb (time, y, x) float32... &gt;&gt; latent_heat (time, y, x) float32... &gt;&gt; rad_net (time, y, x) float32... &gt;&gt; sw_down (time, y, x) float32... &gt;&gt; precip (time, y, x) float32... &gt;&gt; t1p5m_gb (time, y, x) float32... &gt;&gt; q1p5m_gb (time, y, x) float32... month_length = ds.time.dt.days_in_month month_length &gt;&gt; &lt;xarray.DataArray 'days_in_month' (time: 91632)&gt; &gt;&gt; array([31, 31, 31, ..., 31, 31, 31]) &gt;&gt; Coordinates: &gt;&gt; * 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')</pre><p> 但是,我遇到了這個錯誤:</p><pre> TypeError Traceback (most recent call last) &lt;ipython-input-45-51dd727eba52&gt; in &lt;module&gt; 6 7 # Calculate the weighted average ----&gt; 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) -&gt; 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) -&gt; 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 &lt;dictcomp&gt;(.0) 4845 else: 4846 other_variable = getattr(other, "variable", other) -&gt; 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) -&gt; 1989 if not reflexive 1990 else f(other_data, self_data) 1991 ) TypeError: ufunc multiply cannot use operands with types dtype('&lt;M8[ns]') and dtype('float64')</pre><p> 我應該如何轉換我的時間變量以使其工作? 對此問題的任何幫助將不勝感激 - 謝謝!</p><p> 有關時間變量的更多詳細信息:</p><pre> ds.time &gt;&gt; &lt;xarray.DataArray 'time' (time: 91632)&gt; &gt;&gt; array(['1996-07-18T01:00:00.000000000', '1996-07-18T02:00:00.000000000', &gt;&gt; '1996-07-18T03:00:00.000000000', ..., '2006-12-30T22:00:00.000000000', &gt;&gt; '2006-12-30T23:00:16.000000000', '2006-12-31T00:00:00.000000000'], &gt;&gt; dtype='datetime64[ns]') &gt;&gt; Coordinates: &gt;&gt; * time (time) datetime64[ns] 1996-07-18T01:00:00... 2006-12-31 &gt;&gt; Attributes: &gt;&gt; standard_name: time &gt;&gt; long_name: Time of data &gt;&gt; bounds: time_bounds</pre></div></m8[ns]')> 為什么我看到“ufunc &#39;subtract&#39; 不能使用類型為 dtype(&#39; 無法從另一日期時間列減去一個日期時間列,減法不能使用類型為dtype(&#39;S1&#39;)和dtype(&#39; 無法從 dtype(&#39; ARIMA預測:無法使用強制轉換規則&#39;same_kind&#39;將ufunc從dtype(&#39;float64&#39;)減去輸出到dtype(&#39;int64&#39;) 無法計算兩個日期之間的工作日? 轉換 dtype(&#39; 無法使用轉換規則“same_kind”將 ufunc 'add' output 從 dtype('float64') 轉換為 dtype('int64')
 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM