From b00f53da4b092ea13eeeabe92866736e97d56db0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20Capretto?= Date: Tue, 20 Feb 2024 17:39:07 -0300 Subject: [PATCH] Make formulae reach transformations before outer names (#109) --- 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):