wls function: syntax error "unexpected EOF while parsing" occurs when name of dependent variable starts with digits #1441

Closed
kengonn opened this Issue Mar 3, 2014 · 3 comments

Projects

None yet

3 participants

@kengonn
kengonn commented Mar 3, 2014

Here's the code which produces the error:

=== start of code ===
import datetime as dt
from pandas import DataFrame, Series
import statsmodels.formula.api as sm

dates = [dt.datetime(2013, 12, 5), dt.datetime(2013, 12, 6), dt.datetime(2013, 12, 7)]
df_independent_vars = DataFrame([[1., 5.6], [1.5, 8], [2.1, 9]], columns=['b1', 'b2'], index=dates)
ts_dependent_var = Series([25, 32, 45], index=dates, name='2323_HK')
results = sm.wls(formula='2323_HK ~ ' + ' + '.join(df_independent_vars.columns),
data=pd.concat([DataFrame(ts_dependent_var), df_independent_vars], axis=1), weights=[1,1,1]).fit()
print results.params
=== end of code ===

Note that this error goes away when I replace '2323_HK' with 'HK_2323'. For some reason, the parsing mechanism splits '2323_HK' into '2323 HK' (and something else)... if I'm not wrong

@josef-pkt
Member

That's a limitation of patsy's way of implementing formulas
http://patsy.readthedocs.org/en/v0.2.1/builtins-reference.html#patsy.builtins.Q

python variable names cannot start with a number, but I don't know why the specific error message is raised

quoting works for me
results = sm.wls(formula='Q("2323_HK") ~ ' + ' + '.join(df_independent_vars.columns),

If this can be improved, then it should be on the patsy issue tracker
https://github.com/pydata/patsy

@kengonn
kengonn commented Mar 4, 2014

Thanks for your fast reply! got it, and thanks!

PS it would be great if the statsmodels documentation (specifically, in http://statsmodels.sourceforge.net/stable/example_formulas.html#ols-regression-using-formulas) mentioned something about this limitation of variable naming. i couldn't find any mention of this on the web.

@kengonn kengonn closed this Mar 4, 2014
@jseabold
Member
jseabold commented Mar 4, 2014

FAQ candidate for searching later

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment