From 7df836c5a7d4648722ed7ded9927d5f6f2314587 Mon Sep 17 00:00:00 2001 From: Tomas Capretto Date: Tue, 20 Feb 2024 17:32:36 -0300 Subject: [PATCH] Make formulae reach transformations before outer names --- formulae/terms/call.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/formulae/terms/call.py b/formulae/terms/call.py index 2dffa76..a8eed47 100644 --- a/formulae/terms/call.py +++ b/formulae/terms/call.py @@ -8,6 +8,7 @@ from formulae.categorical import ENCODINGS, CategoricalBox, Treatment from formulae.config import config +from formulae.environment import Environment from formulae.transforms import TRANSFORMS, Proportion, Offset from formulae.terms.call_utils import CallVarsExtractor from formulae.utils import is_categorical_dtype @@ -100,8 +101,11 @@ def set_type(self, data_mask, env): env: Environment The environment where values and functions are taken from. """ - - self.env = env.with_outer_namespace({**TRANSFORMS, **ENCODINGS}) + # We initialize an environment where transformations and encodings are available first + # This makes sure formulae uses the internal objects instead of objects that may be + # available in the namespace where 'design_matrices' is called. + transforms_env = Environment([{**TRANSFORMS, **ENCODINGS}]) + self.env = transforms_env.with_outer_namespace(env.namespace) x = self.call.eval(data_mask, self.env) if is_numeric_dtype(x):