pydata / patsy

Describing statistical models in Python using symbolic formulas
Other
954 stars 104 forks source link

SyntaxError: lambda from patsy/eval.py #160

Closed joshuaoco closed 3 years ago

joshuaoco commented 4 years ago

Hi, I was using your library and came across this error, I was wondering if you could tell me whether or not this is an issue with your library, or the implementation of python I'm using (it seems to me to be that one, but I thought I'd ask you guys before the scary python bug request page :) )

File "dissertation.py", line 402, in <module>
    negative_binomial_regressor("Facebook", "share", df_train, df_test)
  File "dissertation.py", line 181, in negative_binomial_regressor
    aux_olsr_results = smf.ols(ols_expr, platform_train).fit()
  File "/usr/lib/python3.8/site-packages/statsmodels/base/model.py", line 168, in from_formula
    tmp = handle_formula_data(data, None, formula, depth=eval_env,
  File "/usr/lib/python3.8/site-packages/statsmodels/formula/formulatools.py", line 64, in handle_formula_data
    result = dmatrices(formula, Y, depth, return_type='dataframe',
  File "/usr/lib/python3.8/site-packages/patsy/highlevel.py", line 309, in dmatrices
    (lhs, rhs) = _do_highlevel_design(formula_like, data, eval_env,
  File "/usr/lib/python3.8/site-packages/patsy/highlevel.py", line 164, in _do_highlevel_design
    design_infos = _try_incr_builders(formula_like, data_iter_maker, eval_env,
  File "/usr/lib/python3.8/site-packages/patsy/highlevel.py", line 66, in _try_incr_builders
    return design_matrix_builders([formula_like.lhs_termlist,
  File "/usr/lib/python3.8/site-packages/patsy/build.py", line 689, in design_matrix_builders
    factor_states = _factors_memorize(all_factors, data_iter_maker, eval_env)
  File "/usr/lib/python3.8/site-packages/patsy/build.py", line 354, in _factors_memorize
    which_pass = factor.memorize_passes_needed(state, eval_env)
  File "/usr/lib/python3.8/site-packages/patsy/eval.py", line 474, in memorize_passes_needed
    subset_names = [name for name in ast_names(self.code)
  File "/usr/lib/python3.8/site-packages/patsy/eval.py", line 474, in <listcomp>
    subset_names = [name for name in ast_names(self.code)
  File "/usr/lib/python3.8/site-packages/patsy/eval.py", line 105, in ast_names
    for node in ast.walk(ast.parse(code)):
  File "/usr/lib/python3.8/ast.py", line 47, in parse
    return compile(source, filename, mode, flags,
  File "<unknown>", line 1
    lambda
matthewwardrop commented 3 years ago

This is a duplicate of #129, but thank you for taking the time to report this issue. The work around of quoting the field name should work also : Q('lambda'). As noted in #129, in Formulaic, the currently in-development successor, this works fine:

from formulaic import Formula
Formula('lambda')  # Outputs: 1 + lambda

Since this project is feature-frozen, I will close this issue for now.

Thanks again!