简体   繁体   English

无法从 dtype('

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

I am using numpy interp to interpolate datapoint but was given Cannot cast array data from dtype('我正在使用 numpy interp 来插入数据点,但得到了无法从 dtype('

Code snippet:代码片段:

import pandas as pd
import numpy as np
def interpolate_fwd_price(row, fx):
    res = np.interp(row['SA_M'], fx['TENOR_DT'], fx['RATE'])
    return res

df = pd.DataFrame({'SA_M': ['2018-02-28','2018-03-10']})
df['SA_M'] = pd.to_datetime(df['SA_M'])
data = pd.DataFrame({'TENOR_DT': ['2017-02-09','2017-03-02','2017-04-03','2017-05-02'], 'RATE':[1.0, 1.2, 1.5, 1.8]})
data['TENOR_DT'] = pd.to_datetime(data['TENOR_DT'])
df['PRICE'] = df.apply(interpolate_fwd_price, fx=data, axis=1)

I did some search and could not figure out what is causing the error.我进行了一些搜索,但无法弄清楚导致错误的原因。 Appreciate your input.感谢您的投入。

Make some change and it works for interpolating the datetime difference instead of datetime directly.进行一些更改,它适用于插入日期时间差异而不是直接插入日期时间。 Would still be interested to know why it did not work for interpolating datetime directly.仍然有兴趣知道为什么它不能直接插入日期时间。

def interpolate_fwd_price(row, fx):
    fx['DT'] = (fx['TENOR_DT'] - row(['SA_M'])).dt.days
    res = np.interp(0, fx['DT'], fx['RATE'])
    return res
In [92]: data = pd.DataFrame({'TENOR_DT': ['2017-02-09','2017-03-02','2017-04-03','2017-05-02'], 'RATE':[1.0, 1.2, 1.5, 1.8]})
In [93]: data        # object dtype with strings
Out[93]: 
   RATE    TENOR_DT
0   1.0  2017-02-09
1   1.2  2017-03-02
2   1.5  2017-04-03
3   1.8  2017-05-02
In [94]: data['TENOR_DT'] = pd.to_datetime(data['TENOR_DT'])
In [95]: data
Out[95]: 
   RATE   TENOR_DT
0   1.0 2017-02-09
1   1.2 2017-03-02
2   1.5 2017-04-03
3   1.8 2017-05-02
In [96]: data['TENOR_DT']
Out[96]: 
0   2017-02-09
1   2017-03-02
2   2017-04-03
3   2017-05-02
Name: TENOR_DT, dtype: datetime64[ns]

The array version of the dates:日期的数组版本:

In [98]: dt = data['TENOR_DT'].values
In [99]: dt
Out[99]: 
array(['2017-02-09T00:00:00.000000000', '2017-03-02T00:00:00.000000000',
       '2017-04-03T00:00:00.000000000', '2017-05-02T00:00:00.000000000'],
      dtype='datetime64[ns]')

It can be cast to float using the default unsafe :可以使用默认的unsafe将其强制转换为浮动:

In [100]: dt.astype(float)
Out[100]: array([1.4865984e+18, 1.4884128e+18, 1.4911776e+18, 1.4936832e+18])
In [101]: dt.astype(float, casting='safe')
TypeError: Cannot cast array from dtype('<M8[ns]') to dtype('float64') according to the rule 'safe'

My guess is that np.interp is using the safe casting to convert those datetime values to floats.我的猜测是np.interp正在使用safe转换将这些日期时间值转换为浮点数。

I haven't tried to do interp with dates before, so can only suggest some fixes.我之前没有尝试过对日期进行interp ,所以只能建议一些修复。 First your dates only differ by day, so we don't need the full ns resolution:首先,您的日期仅因天而异,因此我们不需要完整的ns分辨率:

In [107]: dt.astype('datetime64[D]')
Out[107]: 
array(['2017-02-09', '2017-03-02', '2017-04-03', '2017-05-02'],
      dtype='datetime64[D]')

It still won't allow safe casting, but the 'unsafe' casting produces reasonable looking numbers.它仍然不允许安全铸造,但“不安全”铸造产生合理的数字。 You might be able to use those in the interpolation.您也许可以在插值中使用它们。

In [108]: dt.astype('datetime64[D]').astype(int)
Out[108]: array([17206, 17227, 17259, 17288])

cut function:无法将数组数据从 dtype('float64') 转换为 dtype(' <u32') according to the rule 'safe'< div><div id="text_translate"><p> 我想将 Dataframe 的列中的内容更改为“好”或“坏”。 该列填充了从 1 到 10 的数字。1-5 是坏的,6-10 是好的。 为此,我想使用 cut 方法。</p><pre> bins = (1, 5.5, 10) rating = ['bad', 'good'] game['useropinion'] = pd.cut(rating, bins)</pre><p> 运行后的结果:</p><pre> Cannot cast array data from dtype('float64') to dtype('&lt;U32') according to the rule 'safe'</pre><p> 怎么了? 我如何解决它?</p></div></u32')> - cut function : Cannot cast array data from dtype('float64') to dtype('<U32') according to the rule 'safe'

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 TypeError:无法从dtype(&#39; - TypeError: Cannot cast array data from dtype('<U1') to dtype('float64') according to rule 'safe' 根据规则&#39;safe&#39;,无法将数组数据从dtype(&#39;float64&#39;)转换为dtype(&#39;int32&#39;) - Cannot cast array data from dtype('float64') to dtype('int32') according to the rule 'safe' 类型错误:无法根据规则“安全”将数组数据从 dtype(&#39;O&#39;) 转换为 dtype(&#39;float64&#39;) - TypeError: Cannot cast array data from dtype('O') to dtype('float64') according to the rule 'safe' 无法根据规则“安全”将数组数据从 dtype(&#39;O&#39;) 转换为 dtype(&#39;float64&#39;) - Cannot cast array data from dtype('O') to dtype('float64') according to the rule 'safe' 无法根据规则“安全”将数组数据从dtype(&#39;complex128&#39;)强制转换为dtype(&#39;float64&#39;) - Cannot cast array data from dtype('complex128') to dtype('float64') according to the rule 'safe' odeint:无法根据规则“安全”将数组数据从 dtype(&#39;O&#39;) 转换为 dtype(&#39;float64&#39;) - odeint: Cannot cast array data from dtype('O') to dtype('float64') according to the rule 'safe' TypeError:无法将数组数据从 dtype(&#39;O&#39;) 转换为 dtype(&#39; - TypeError: Cannot cast array data from dtype('O') to dtype('<M8[D]') according to the rule 'safe' 我收到此错误消息:无法根据规则“安全”将数组数据从 dtype('O') 转换为 dtype('float64') - I get this error message: Cannot cast array data from dtype('O') to dtype('float64') according to the rule 'safe' cut function:无法将数组数据从 dtype('float64') 转换为 dtype(' <u32') according to the rule 'safe'< div><div id="text_translate"><p> 我想将 Dataframe 的列中的内容更改为“好”或“坏”。 该列填充了从 1 到 10 的数字。1-5 是坏的,6-10 是好的。 为此,我想使用 cut 方法。</p><pre> bins = (1, 5.5, 10) rating = ['bad', 'good'] game['useropinion'] = pd.cut(rating, bins)</pre><p> 运行后的结果:</p><pre> Cannot cast array data from dtype('float64') to dtype('&lt;U32') according to the rule 'safe'</pre><p> 怎么了? 我如何解决它?</p></div></u32')> - cut function : Cannot cast array data from dtype('float64') to dtype('<U32') according to the rule 'safe' Numpy.dot TypeError:根据规则&#39;safe&#39;,无法将数组数据从dtype(&#39;float64&#39;)转换为dtype(&#39;S32&#39;) - Numpy.dot TypeError: Cannot cast array data from dtype('float64') to dtype('S32') according to the rule 'safe'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM