Closed WojtAcht closed 2 weeks ago
Hey @WojtAcht! I'm here to help you out with any issues you're facing. Let's tackle this bug together and get it resolved!
To resolve the NameError: name 'calculate_salary_betas' is not defined
error, you need to update the _extract_fix_dataframe_redeclarations
method to include skill definitions in its execution environment. Here is the updated method:
def _extract_fix_dataframe_redeclarations(
self, node: ast.AST, code_lines: list[str]
) -> ast.AST:
if isinstance(node, ast.Assign):
target_names, is_slice, target = self._get_target_names(node.targets)
if target_names and self._check_is_df_declaration(node):
# Construct dataframe from node
code = "\n".join(code_lines)
env = get_environment(self._additional_dependencies)
env["dfs"] = copy.deepcopy(self._get_originals(self._dfs))
# Include skills in the environment
for skill_name, skill_func in self.skills_manager.skills.items():
env[skill_name] = skill_func
exec(code, env)
df_generated = (
env[target_names[0]][target.slice.value]
if is_slice
else env[target_names[0]]
)
# check if exists in provided dfs
for index, df in enumerate(self._dfs):
head = df.get_head()
if head.shape == df_generated.shape and head.columns.equals(
df_generated.columns
):
target_var = (
ast.Subscript(
value=ast.Name(id=target_names[0], ctx=ast.Load()),
slice=target.slice,
ctx=ast.Store(),
)
if is_slice
else ast.Name(id=target_names[0], ctx=ast.Store())
)
return ast.Assign(
targets=[target_var],
value=ast.Subscript(
value=ast.Name(id="dfs", ctx=ast.Load()),
slice=ast.Index(value=ast.Num(n=index)),
ctx=ast.Load(),
),
)
return None
This modification ensures that the skills managed by SkillsManager
are included in the execution environment, thus avoiding the NameError
[1].
To continue talking to Dosu, mention @dosu.
System Info
OS version: macOS 14.5 Python version: Python 3.10.7 The current version of pandasai being used: 2.2.12
🐛 Describe the bug
Bug: Skill Calculations Fail in PandasAI
Issue Description
Skills that perform calculations are failing with a
NameError: name '<skill>' is not defined
error. This occurs because the_extract_fix_dataframe_redeclarations
method executes code in an environment that lacks skill definitions.Root Cause
The
_extract_fix_dataframe_redeclarations
method uses an environment created byget_environment()
, which does not include skill definitions:The
get_environment()
function returns a dictionary with pandas, matplotlib, numpy, and some whitelisted builtins, but no skills:Contrast with Correct Implementation
In contrast, the
execute_code
method in theCodeExecution
class correctly adds skills to the environment:Proposed Solution
To fix this issue, the
_extract_fix_dataframe_redeclarations
method should be updated to include skill definitions in its execution environment, similar to theexecute_code
method.Example
Error: