Repository navigation
BUG: Evaluate second formulas with the model's eval_env - #11225
Merged
bashtage merged 6 commits intoNov 26, 2026
Merged
Conversation
`eval_env` in `from_formula` selects the namespace in which variables of the formula that are not in the data are looked up. An integer meant a different namespace in different models: - `Model.from_formula` (`ols`, `glm`, `logit`, ...): 1 is the namespace that calls `from_formula`, 2 the one above it. - Models whose `from_formula` calls their parent's through `advance_eval_env` (GEE, NominalGEE, OrdinalGEE, PHReg, MixedLM, ConditionalLogit, ConditionalMNLogit, ConditionalPoisson, QIF, OrderedModel, BetaModel): one frame lower, so 1 looked inside statsmodels and 2 was the calling namespace. In MixedLM, `re_formula` and `vc_formula` already counted like `Model.from_formula`, so one integer meant two namespaces in the same model. - `RollingOLS.from_formula`: one frame higher, so 0 was the calling namespace and the default (None) silently looked in the namespace above the caller, where a variable of the same name would be used. A dict raised a TypeError. The docstrings said that "the default ``eval_env=0`` uses the calling namespace", which was true of no model. Behavior changes - Every model follows `Model.from_formula`: None or 1 is the namespace that calls `from_formula`, an integer k the namespace k stack frames above it, -1 an empty namespace, and a dict holds the variables. Code that passed an integer to the models above now refers to another namespace (pass 1 instead of 2, or None, for the calling namespace). - `advance_eval_env` moves integers up a frame, as it did for None. - `RollingOLS.from_formula` uses the calling namespace by default, counts integers like the other models, and accepts a dict. - `numpy` integers count like Python integers (they were not moved up in `Model.from_formula`). Not changed - None, -1 and dicts mean what they meant in every model except `RollingOLS` (above), and variables in the data are found first, so a formula whose variables are all in the data gives the same model. Tests - For both formula engines and `ols`, `gee`, `phreg`, `mixedlm` (formula and `re_formula`), `conditional_logit`, `QIF`, `OrderedModel`, `BetaModel` and `RollingOLS`: a variable defined in the calling namespace and, with other values, one frame above it is taken from the calling namespace for None, 1 and `np.int64(1)`, and from the frame above for 2; a dict is used, and a name in neither the data nor the dict is not found. The integer cases of every model but `ols`, and the default of `RollingOLS`, fail on main.
The Namespaces section of the formula guide suggested passing a patsy EvalEnvironment, which is deprecated as an evaluation environment, and spelled the keyword as eval_func. It now lists what eval_env accepts (None or 1 for the calling namespace, an integer number of stack frames, a dict, and -1 for an empty namespace), says that it means the same in every model, and that variables in the data are found first. Documentation only.
`RollingOLS.from_formula` and the `re_formula` and `vc_formula` of `MixedLM.from_formula` call `FormulaManager.get_matrices` in their own body, and each translated `eval_env` for it with the same few lines (None is the calling namespace, -1 an empty namespace). A helper, `statsmodels.formula.formulatools.get_matrices_eval_env`, does it once, as `advance_eval_env` does for a call to the parent's `from_formula`. Other `from_formula` methods that evaluate their own formulas use it in the follow-up pull requests. No change in behavior: the tests of the previous commit and the MixedLM and RollingOLS suites pass unchanged.
`BetaModel.from_formula` with `exog_precision_formula` and a `subset` keyword (even `subset=None`) raised `AttributeError: 'DataFrame' object has no attribute 'ix'`: the rows for the precision formula were selected with `DataFrame.ix`, which pandas removed in 1.0. Changes - The precision formula is evaluated on `data.loc[subset]`, the rows the formula of the mean uses, or on all of `data` when `subset` is None. Not changed - Without `subset`, the model is the same as before. Tests - With a subset of 20 of 30 rows and with `subset=None`: the design of the mean and of the precision equal the rows of the data written out by hand. Both fail on main.
Some `from_formula` methods evaluate further formulas themselves. These ignored `eval_env` and looked variables that are not in the data up in statsmodels' own code, so they were not found: - `BetaModel.from_formula`: `exog_precision_formula`. - `ProcessMLE.from_formula`: `scale_formula`, `smooth_formula` and `noise_formula`, and also the mean `formula`, because `eval_env` was not passed on at all. - `GEE.from_formula` (and NominalGEE, OrdinalGEE): `dep_data` given as a formula. Changes - These formulas use the same namespace as the model's formula: None or 1 is the namespace that calls `from_formula`, an integer k the namespace k stack frames above it, -1 an empty namespace, and a dict holds the variables. - They use `get_matrices_eval_env` for this, and `ProcessMLE` passes `eval_env` on to the mean formula with `advance_eval_env`. - The docstrings say so; `ProcessMLE.from_formula` documents `eval_env`. Not changed - Variables in the data are found first, so formulas that worked before give the same matrices. Tests - For both engines and each of these formulas: a variable defined in the calling namespace and, with other values, one frame above it, is taken from the calling namespace for None and 1 and from the frame above for 2; a dict is used; -1 finds nothing. All fail on main. - `get_matrices_eval_env(-1)` is an empty namespace (`pd`, which statsmodels' own code has, is not found) and None is the calling namespace.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Stacked on #11223, merge that first (its three commits are the first ones here).
BUG: Give eval_env the same meaning in every from_formula
eval_envinfrom_formulaselects the namespace in which variables ofthe formula that are not in the data are looked up. An integer meant a
different namespace in different models:
Model.from_formula(ols,glm,logit, ...): 1 is the namespacethat calls
from_formula, 2 the one above it.from_formulacalls their parent's throughadvance_eval_env(GEE, NominalGEE, OrdinalGEE, PHReg, MixedLM,ConditionalLogit, ConditionalMNLogit, ConditionalPoisson, QIF,
OrderedModel, BetaModel): one frame lower, so 1 looked inside
statsmodels and 2 was the calling namespace. In MixedLM,
re_formulaand
vc_formulaalready counted likeModel.from_formula, so one integermeant two namespaces in the same model.
RollingOLS.from_formula: one frame higher, so 0 was the callingnamespace and the default (None) silently looked in the namespace above
the caller, where a variable of the same name would be used. A dict
raised a TypeError.
The docstrings said that "the default
eval_env=0uses the callingnamespace", which was true of no model.
Behavior changes
Model.from_formula: None or 1 is the namespacethat calls
from_formula, an integer k the namespace k stack framesabove it, -1 an empty namespace, and a dict holds the variables. Code
that passed an integer to the models above now refers to another
namespace (pass 1 instead of 2, or None, for the calling namespace).
advance_eval_envmoves integers up a frame, as it did for None.RollingOLS.from_formulauses the calling namespace by default, countsintegers like the other models, and accepts a dict.
numpyintegers count like Python integers (they were not moved up inModel.from_formula).Not changed
RollingOLS(above), and variables in the data are found first, so aformula whose variables are all in the data gives the same model.
Tests
ols,gee,phreg,mixedlm(formulaand
re_formula),conditional_logit,QIF,OrderedModel,BetaModelandRollingOLS: a variable defined in the callingnamespace and, with other values, one frame above it is taken from the
calling namespace for None, 1 and
np.int64(1), and from the frame abovefor 2; a dict is used, and a name in neither the data nor the dict is not
found. The integer cases of every model but
ols, and the default ofRollingOLS, fail on main.DOC: Describe eval_env in the formula guide
The Namespaces section of the formula guide suggested passing a patsy EvalEnvironment, which is deprecated as an evaluation environment, and spelled the keyword as eval_func. It now lists what eval_env accepts (None or 1 for the calling namespace, an integer number of stack frames, a dict, and -1 for an empty namespace), says that it means the same in every model, and that variables in the data are found first.
Documentation only.
REF: Translate eval_env for get_matrices in one place
RollingOLS.from_formulaand there_formulaandvc_formulaofMixedLM.from_formulacallFormulaManager.get_matricesin their ownbody, and each translated
eval_envfor it with the same few lines (Noneis the calling namespace, -1 an empty namespace). A helper,
statsmodels.formula.formulatools.get_matrices_eval_env, does it once, asadvance_eval_envdoes for a call to the parent'sfrom_formula. Otherfrom_formulamethods that evaluate their own formulas use it in thefollow-up pull requests.
No change in behavior: the tests of the previous commit and the MixedLM and
RollingOLS suites pass unchanged.
BUG: Select the subset rows for BetaModel's precision formula
BetaModel.from_formulawithexog_precision_formulaand asubsetkeyword (even
subset=None) raisedAttributeError: 'DataFrame' object has no attribute 'ix': the rows forthe precision formula were selected with
DataFrame.ix, which pandasremoved in 1.0.
Changes
data.loc[subset], the rows theformula of the mean uses, or on all of
datawhensubsetis None.Not changed
subset, the model is the same as before.Tests
subset=None: the design of themean and of the precision equal the rows of the data written out by hand.
Both fail on main.
BUG: Evaluate second formulas with the model's eval_env
Some
from_formulamethods evaluate further formulas themselves. Theseignored
eval_envand looked variables that are not in the data up instatsmodels' own code, so they were not found:
BetaModel.from_formula:exog_precision_formula.ProcessMLE.from_formula:scale_formula,smooth_formulaandnoise_formula, and also the meanformula, becauseeval_envwas notpassed on at all.
GEE.from_formula(and NominalGEE, OrdinalGEE):dep_datagiven as aformula.
Changes
is the namespace that calls
from_formula, an integer k the namespace kstack frames above it, -1 an empty namespace, and a dict holds the
variables.
get_matrices_eval_envfor this, andProcessMLEpasseseval_envon to the mean formula withadvance_eval_env.ProcessMLE.from_formuladocumentseval_env.Not changed
give the same matrices.
Tests
For both engines and each of these formulas: a variable defined in the
calling namespace and, with other values, one frame above it, is taken
from the calling namespace for None and 1 and from the frame above for 2;
a dict is used; -1 finds nothing. All fail on main.
get_matrices_eval_env(-1)is an empty namespace (pd, whichstatsmodels' own code has, is not found) and None is the calling
namespace.
tests added / passed.
code/documentation is well formatted.
properly formatted commit message. See
NumPy's guide.
AI Disclosure
Contributions must comply with the statsmodels AI Policy.
Tick exactly one of the following. If AI tools were used, replace the
<...>placeholders in the second option with the tool name(s) and how the tool's output was
used.
Tool(s): Claude Code (Claude Opus 5.5).
Used for: mapping which namespace eval_env selects in every from_formula, writing the change and its tests, and checking them (tests failing on main, a mutation check of the changed lines, and the tests in the oldest, middle, latest and formulaic-only environments).
I have personally read, understood, and can explain every line of this diff, and
have verified the statistical/numerical correctness of the change.
🤖 Generated with Claude Code