使用数据帧数据调用函数会产生错误(无法将系列转换为<class'flove'=“”>)

时间:2015-06-14 00:23:29

标签: python pandas dataframe

我有一个选项定价模型(非常简单的Black Scholes),它可以很好地处理数据:

Black Sholes Function

def BS2(S,X,T,r,v):
    d1 = (log(S/X)+(.001+v*v/2)*T)/(v*sqrt(T))
    d2 = d1-v*sqrt(T)
    return (S*CND(d1)-X*exp(-.001*T)*CND(d2))

功能在这里:

Cumulative normal distribution function

def CND(X):
    (a1,a2,a3,a4,a5) = (0.31938153, -0.356563782, 1.781477937, 
     -1.821255978, 1.330274429)
    L = abs(X)
    K = 1.0 / (1.0 + 0.2316419 * L)
    w = 1.0 - 1.0 / sqrt(2*pi)*exp(-L*L/2.) * (a1*K + a2*K*K + a3*pow(K,3) +
    a4*pow(K,4) + a5*pow(K,5))
    if X<0:
        w = 1.0-w
    return w

我认为这个问题不重要,但BS2称之为:

def BS(df):
    d1 = (log(S/X)+(.001+v*v/2)*T)/(v*sqrt(T))
    d2 = d1-v*sqrt(T)
    return pd.Series((S*CND(d1)-X*exp(-.001*T)*CND(d2)))

我试图修改工作BS功能以接受来自df的数据,但似乎做错了什么:

In [13]:
df

Out[13]:
    S    X   T    r    v
0  100  100  1  0.001  0.3
1   50   50  1  0.001  0.3

我的数据很直接:

In [14]:

df.dtypes
Out[14]:
S    float64
X    float64
T    float64
r    float64
v    float64
dtype: object

并且都是float64

S=df['S']
X=df['X']
T=df['T']
r=df['r']
v=df['v']

我也尝试在发送给BS2之前将df变量分配给一个名称(我这样做了,没有这个分配:

In [18]:

BS(df)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-18-745e7dd0eb2c> in <module>()
----> 1 BS(df)

<ipython-input-17-b666a39cd530> in BS(df)
  3 def BS(df):
  4     CallPutFlag='c'
----> 5     d1 = (log(S/X)+(.001+v*v/2)*T)/(v*sqrt(T))
  6     d2 = d1-v*sqrt(T)
  7     cp = ((S*CND(d1)-X*exp(-.001*T)*CND(d2)))

C:\Users\camcompco\AppData\Roaming\Python\Python34\site-   packages\pandas\core\series.py in wrapper(self)
 74             return converter(self.iloc[0])
 75         raise TypeError(
---> 76             "cannot convert the series to {0}".format(str(converter)))
 77     return wrapper
 78 

TypeError: cannot convert the series to <class 'float'>

冒着发送太多信息的风险,以下是错误消息:

php -a

非常感谢任何协助。

约翰

2 个答案:

答案 0 :(得分:1)

我认为使用dataframe.apply()

会更容易

http://pandas.pydata.org/pandas-docs/dev/generated/pandas.DataFrame.apply.html

然后语法将 EDIT ld: warning: directory not found for option '- L/Users/welch/Downloads/Flurry' ld: warning: directory not found for option '-LiPhone' ld: warning: directory not found for option '-LSDK' ld: warning: directory not found for option '-LviPhone' ld: warning: directory not found for option '-L6.4.0' ld: warning: directory not found for option '-L(1)/Flurry-iOS-6.4.0/Flurry' ld: warning: directory not found for option '-L(1)/Flurry-iOS-6.4.0/FlurryAds' ld: warning: directory not found for option '-F(1)' ld: warning: directory not found for option '-F/Users/welch/Downloads/IOS-InApp' ld: warning: directory not found for option '-FSDK-InApp-2.4.2' ld: library not found for -lFlurryAds_6.4.0 clang: error: linker command failed with exit code 1 (use -v to see invocation) ld: warning: directory not found for option '-L/Users/welch/Downloads/Flurry' ld: warning: directory not found for option '-LiPhone' ld: warning: directory not found for option '-LSDK' ld: warning: directory not found for option '-LviPhone' ld: warning: directory not found for option '-L6.4.0' ld: warning: directory not found for option '-L(1)/Flurry-iOS-6.4.0/Flurry' ld: warning: directory not found for option '-L(1)/Flurry-iOS-6.4.0/FlurryAds' ld: warning: directory not found for option '-F(1)' ld: warning: directory not found for option '-F/Users/welch/Downloads/IOS-InApp' ld: warning: directory not found for option '-FSDK-InApp-2.4.2' ld: library not found for -lFlurryAds_6.4.0 clang: error: linker command failed with exit code 1 (use -v to see invocation) 将函数func应用于每一行。

这个问题的答案是相似的:

Apply function to each row of pandas dataframe to create two new columns

答案 1 :(得分:0)

@ JonD的答案很好,但如果数据帧有多行,这里的替代答案会更快:

gcc -o p_rec.exe C:\path_to_portaudio\portaudio\common\pa_ringbuffer.c \
paex_record_file.c -lportaudio \
-IC:\path_to_portaudio\portaudio\src\common

的变化:

  1. 重点是矢量化功能。语法方面,主要的变化是明确使用ifdef#ifdef _WIN32 #include <windows.h> #include <process.h> #endif #include <windows.h> #include <process.h> 的numpy版本。否则你不必改变太多,因为numpy / pandas以元素方式支持基本的数学运算。
  2. 将来自scipy的用户编写的CND替换为from scipy.stats import norm def BS2(df): d1 = (np.log(df.S/df.X)+(.001+df.v*df.v/2)*df['T'])/(df.v*np.sqrt(df['T'])) d2 = d1-df.v*np.sqrt(df['T']) return (df.S*norm.cdf(d1)-df.X*np.exp(-.001*df['T'])*norm.cdf(d2)) 。更快的内置函数b / c几乎总是尽可能快。
  3. 这是次要的,但我使用了sqrt和其他人的快捷符号,但log需要写出来,因为exp将被解释为norm.cdf。我想这是一个很好的例子,为什么你应该避免使用快捷符号,但我很懒...
  4. 不过,如果你想要更快的速度,接下来要做的就是在numpy而不是pandas中做。您还可以检查其他人是否已经编写过Black-Scholes函数/库(可能,虽然我对此一无所知)。