run-llama / llama_index

LlamaIndex is a data framework for your LLM applications
https://docs.llamaindex.ai
MIT License
35.47k stars 5.01k forks source link

[Question]: name 'np' is not defined while using PandasAgent #14750

Open Shubh789da opened 2 months ago

Shubh789da commented 2 months ago

Question Validation

Question

while asking a question the pandas agend used np and got the below error

Traceback (most recent call last):

File "C:\Users\shubhanshu\anaconda3\Lib\site-packages\llama_index\experimental\query_engine\pandas\output_parser.py", line 42, in default_output_processor safe_exec(ast.unparse(module), {}, local_vars) # type: ignore ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\shubhanshu\anaconda3\Lib\site-packages\llama_index\experimental\exec_utils.py", line 171, in safe_exec return exec(__source, _get_restricted_globals(globals), locals) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "", line 1, in NameError: name 'np' is not defined

below is the possible line where the error occured

Running module pandas_output_parser with input: input: assistant: df['enrollmentCount'] = df['enrollmentCount'].replace('', np.nan)

the libraries I have imported

from llama_index.core.query_pipeline import ( QueryPipeline as QP, Link, InputComponent, ) from llama_index.experimental.query_engine.pandas import ( PandasInstructionParser, ) from llama_index.llms.openai import OpenAI from llama_index.core import PromptTemplate import pandas as pd import numpy as np

although adding np: to the loca_vars at line 32 in output_parser.py resolved this problme but I wanted to ask does it has any permanent solution by default or any better way to resolve this as currently its running in my local but I want to deploy in streamlit so changing wil local_vars will be difficult

the resolved is local_vars = {"df": df,"np":np}

and this issue helped to resolve 12239

dosubot[bot] commented 2 months ago

Hey @Shubh789da! I'm a bot designed to help you solve bugs, answer questions, and become a contributor. I'm here to assist you while waiting for a human maintainer. How can I assist you today?

To resolve the NameError: name 'np' is not defined issue in PandasAgent without manually modifying local_vars, you should ensure that the necessary libraries are imported at the beginning of your script. Specifically, you need to import the numpy library as np and the pandas library as pd.

Here is a permanent solution:

import numpy as np
import pandas as pd

# Your existing code here

By including these import statements, you ensure that np and pd are defined and available for use throughout your script, thus avoiding the NameError.

Additionally, if you are using LlamaIndex (GPT Index) and encountering this issue, make sure that the imports are correctly placed in the relevant sections of your code, especially in any custom scripts or notebooks you are working with. This will help prevent the NameError when running Pandas instructions or any other code that relies on these libraries [1][2].

To continue talking to Dosu, mention @dosu.