Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
ghost opened this issue Mar 3, 2014 · 3 comments

Comments

Projects
None yet
2 participants
@ghost
Copy link

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

This comment has been minimized.

Copy link
Member

commented Mar 3, 2014

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

@ghost

This comment has been minimized.

Copy link
Author

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.

@ghost ghost closed this Mar 4, 2014

@jseabold

This comment has been minimized.

Copy link
Member

commented Mar 4, 2014

FAQ candidate for searching later

This issue was closed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.