'Series' object is not callable Error / Statsmodels illegal variable name
'Series' object is not callable Error / Statsmodels illegal variable name
Update:
You can download my dataset here.
I am running a simple OLS regression with statsmodels and pandas dataframe as following:
import statsmodels.formula.api as sm
import pandas as pd
df=pd.read_csv("exp.csv")
#df is a dataframe that I have containing many variable names such as AAPL, SPY, INF, etc.
for column in df:
result=sm.ols(formula="SPY"+" ~ "+column, data=df).fit()
However, one of the column name in df is INF
. I guess maybe INF
is a reserved word for pasty, the code gives me the following error:
INF
INF
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/ap248/.local/easybuild/software/2017/Core/miniconda2/4.3.27/lib/python2.7/site-packages/statsmodels/base/model.py", line 155, in from_formula
missing=missing)
File "/home/ap248/.local/easybuild/software/2017/Core/miniconda2/4.3.27/lib/python2.7/site-packages/statsmodels/formula/formulatools.py", line 65, in handle_formula_data
NA_action=na_action)
File "/home/ap248/.local/easybuild/software/2017/Core/miniconda2/4.3.27/lib/python2.7/site-packages/patsy/highlevel.py", line 310, in dmatrices
NA_action, return_type)
File "/home/ap248/.local/easybuild/software/2017/Core/miniconda2/4.3.27/lib/python2.7/site-packages/patsy/highlevel.py", line 165, in _do_highlevel_design
NA_action)
File "/home/ap248/.local/easybuild/software/2017/Core/miniconda2/4.3.27/lib/python2.7/site-packages/patsy/highlevel.py", line 62, in _try_incr_builders
formula_like = ModelDesc.from_formula(formula_like)
File "/home/ap248/.local/easybuild/software/2017/Core/miniconda2/4.3.27/lib/python2.7/site-packages/patsy/desc.py", line 165, in from_formula
value = Evaluator().eval(tree, require_evalexpr=False)
File "/home/ap248/.local/easybuild/software/2017/Core/miniconda2/4.3.27/lib/python2.7/site-packages/patsy/desc.py", line 400, in eval
result = self._evaluators[key](self, tree)
File "/home/ap248/.local/easybuild/software/2017/Core/miniconda2/4.3.27/lib/python2.7/site-packages/patsy/desc.py", line 221, in _eval_any_tilde
exprs = [evaluator.eval(arg) for arg in tree.args]
File "/home/ap248/.local/easybuild/software/2017/Core/miniconda2/4.3.27/lib/python2.7/site-packages/patsy/desc.py", line 400, in eval
result = self._evaluators[key](self, tree)
File "/home/ap248/.local/easybuild/software/2017/Core/miniconda2/4.3.27/lib/python2.7/site-packages/patsy/desc.py", line 355, in _eval_number
"only allowed with **", tree)
patsy.PatsyError: numbers besides '0' and '1' are only allowed with **
SPY ~ INF
^^^
I have also tried using the Q function:
result=sm.ols(formula="SPY"+" ~ "+"Q('INF')", data=df).fit()
However, it gives me the following error instead:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/ap248/.local/easybuild/software/2017/Core/miniconda2/4.3.27/lib/python2.7/site-packages/statsmodels/base/model.py", line 155, in from_formula
missing=missing)
File "/home/ap248/.local/easybuild/software/2017/Core/miniconda2/4.3.27/lib/python2.7/site-packages/statsmodels/formula/formulatools.py", line 65, in handle_formula_data
NA_action=na_action)
File "/home/ap248/.local/easybuild/software/2017/Core/miniconda2/4.3.27/lib/python2.7/site-packages/patsy/highlevel.py", line 310, in dmatrices
NA_action, return_type)
File "/home/ap248/.local/easybuild/software/2017/Core/miniconda2/4.3.27/lib/python2.7/site-packages/patsy/highlevel.py", line 165, in _do_highlevel_design
NA_action)
File "/home/ap248/.local/easybuild/software/2017/Core/miniconda2/4.3.27/lib/python2.7/site-packages/patsy/highlevel.py", line 70, in _try_incr_builders
NA_action)
File "/home/ap248/.local/easybuild/software/2017/Core/miniconda2/4.3.27/lib/python2.7/site-packages/patsy/build.py", line 696, in design_matrix_builders
NA_action)
File "/home/ap248/.local/easybuild/software/2017/Core/miniconda2/4.3.27/lib/python2.7/site-packages/patsy/build.py", line 443, in _examine_factor_types
value = factor.eval(factor_states[factor], data)
File "/home/ap248/.local/easybuild/software/2017/Core/miniconda2/4.3.27/lib/python2.7/site-packages/patsy/eval.py", line 566, in eval
data)
File "/home/ap248/.local/easybuild/software/2017/Core/miniconda2/4.3.27/lib/python2.7/site-packages/patsy/eval.py", line 551, in _eval
inner_namespace=inner_namespace)
File "/home/ap248/.local/easybuild/software/2017/Core/miniconda2/4.3.27/lib/python2.7/site-packages/patsy/compat.py", line 36, in call_and_wrap_exc
return f(*args, **kwargs)
File "/home/ap248/.local/easybuild/software/2017/Core/miniconda2/4.3.27/lib/python2.7/site-packages/patsy/eval.py", line 166, in eval
+ self._namespaces))
File "<string>", line 1, in <module>
TypeError: 'Series' object is not callable
Any idea how to solve it?
1 Answer
1
According to this link: http://patsy.readthedocs.io/en/latest/builtins-reference.html#patsy.builtins.Q you can use Q("var") in the formula to get rid of the error.
The following code should work.
model = sm.ols('SPY ~ Q("INF")',data=df).fit()
A full example
import statsmodels.formula.api as sm
import pandas as pd
import numpy as np
a = np.random.rand(10,3)
df = pd.DataFrame(data=a, columns=['SPY','INF','X'])
model = sm.ols('SPY ~ Q("INF")',data=df).fit()
You can download the dataset here: dropbox.com/s/47p66s718fcgjve/exp.csv?dl=0
– Jinhua Wang
20 mins ago
It looks like the Q function gives an error in this case ...
– Jinhua Wang
20 mins ago
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
But why did it give me the series object not callable error priviously? The code seems the same to me.
– Jinhua Wang
1 hour ago