无法根据规则“安全"将数组数据从dtype('< M8 [ns]')转换为dtype('float64') [英] Cannot cast array data from dtype('<M8[ns]') to dtype('float64') according to the rule 'safe'

查看:228
本文介绍了无法根据规则“安全"将数组数据从dtype('< M8 [ns]')转换为dtype('float64')的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用numpy interp插值数据点,但无法从dtype('

代码段:

I am using numpy interp to interpolate datapoint but was given Cannot cast array data from 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.

进行一些更改,它可用于插补datetime差异,而不是直接插值datetime.仍然有兴趣知道为什么它不能直接用于插值日期时间.

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]

日期的数组版本:

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]')

可以使用默认的unsafe将其强制转换为浮动形式:

It can be cast to float using the default 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'

我的猜测是np.interp正在使用safe强制转换将那些日期时间值转换为浮点数.

My guess is that np.interp is using the safe casting to convert those datetime values to floats.

我还没有尝试使用日期做过interp,因此只能提出一些解决方案.首先,您的日期仅因天而异,因此我们不需要完整的ns分辨率:

I haven't tried to do interp with dates before, so can only suggest some fixes. First your dates only differ by day, so we don't need the full ns resolution:

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])

这篇关于无法根据规则“安全"将数组数据从dtype('&lt; M8 [ns]')转换为dtype('float64')的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆