问题
I've been getting the error: TypeError: float() argument must be a string or a number, not 'method'. Below is my snippet of code.
I've checked other posts like this one: TypeError: float() argument must be a string or a number, not 'function' – Python/Sklearn but can't seem to get to the root cause of the error. Is python saying that my variables (y, x1, x2 etc.) are 'methods' which is why I'm receiving the error?
If so, does anyone know how I can resolve this? Thanks in advance to anyone that can help me!
# Replace inf and NaNs
df_raw['daily pct return']= df_raw['daily pct return'].replace([np.inf, -np.inf],np.nan).dropna
df_raw = pd.DataFrame(data=df_raw)
df_raw.to_csv('Raw_final.csv', header=True)
y = (df_raw['daily pct return'].shift(periods=1)).astype(float)
x1 = (df_raw['Excess daily return']).astype(float)
x2 = (df_raw['Excess weekly return']).astype(float)
x3 = (df_raw['Excess monthly return']).astype(float)
x4 = (df_raw['Trading vol / mkt cap']).astype(float)
x5 = (df_raw['Std dev']).astype(float)
x6 = (df_raw['Residual risk']).astype(float)
result = smf.OLS(exog=y, endog=[x1, x2, x3, x4, x5, x6]).fit()
print(result.params)
print(result.summary())
回答1:
Typo:
df_raw['daily pct return'] = df_raw['daily pct return'].replace(...).dropna()
In the very first statement of your code, you are assigning to 'daily pct return'
column the dropna
method of the dataframe. You probably want it to be the output of the method instead. To achieve this, simply add a pair of parenthesis to the end of the statement, so the interpreter understands you are calling the method to use its output, and not referring to the method itself as to an object.
来源:https://stackoverflow.com/questions/53664687/typeerror-float-argument-must-be-a-string-or-a-number-not-method-multipl