pandas-ai icon indicating copy to clipboard operation
pandas-ai copied to clipboard

Issue with PandasAI: KeyError: '__import__' during Code Execution

Open rakendd opened this issue 5 months ago • 2 comments

System Info

pandas 2.2.14 in python3.11.0 Ubuntu 22.04.4 LTS

🐛 Describe the bug

Hi everyone,

I'm encountering an issue while using PandasAI to generate and execute some code. However, during the execution step, I'm getting the following error:

2024-08-29 08:00:29 [ERROR] Failed with error: Traceback (most recent call last): File "/home/rakend/anaconda3/envs/test_snowflakedata/lib/python3.11/site-packages/pandasai/pipelines/chat/code_execution.py", line 85, in execute result = self.execute_code(code_to_run, code_context) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/rakend/anaconda3/envs/test_snowflakedata/lib/python3.11/site-packages/pandasai/pipelines/chat/code_execution.py", line 171, in execute_code exec(code, environment) File "", line 12, in File "pandas/_libs/tslibs/timestamps.pyx", line 1054, in pandas._libs.tslibs.timestamps._Timestamp.repr File "pandas/_libs/tslibs/timestamps.pyx", line 1496, in pandas._libs.tslibs.timestamps.Timestamp.strftime KeyError: 'import'

Code PandasAI generated and attempted to run

df = dfs[0]
df['EXECUTION_DATE'] = pd.to_datetime(df['EXECUTION_DATE'])
df['EFFICIENCY_PERCENTAGE'] = pd.to_numeric(df['EFFICIENCY_PERCENTAGE'], errors='coerce')
df['WAREHOUSE_USAGE_COST'] = pd.to_numeric(df['WAREHOUSE_USAGE_COST'], errors='coerce')
df['QUERY_EXECUTION_COST'] = pd.to_numeric(df['QUERY_EXECUTION_COST'], errors='coerce')
grouped_df = df.groupby(['EXECUTION_DATE', 'WAREHOUSE_NAME']).agg({'EFFICIENCY_PERCENTAGE': 'mean', 'WAREHOUSE_USAGE_COST': 'sum'}).reset_index()
filtered_df = grouped_df[grouped_df['WAREHOUSE_NAME'] == 'MEMBER_PROFILE_DE_WH_L']
filtered_df['WAREHOUSE_USAGE_COST_PCT_CHANGE'] = filtered_df['WAREHOUSE_USAGE_COST'].pct_change() * 100
filtered_df['EFFICIENCY_PERCENTAGE_CHANGE'] = filtered_df['EFFICIENCY_PERCENTAGE'].diff()
filtered_df['EFFICIENCY_PERCENTAGE_LABEL'] = 'Daily Average'
filtered_df['WAREHOUSE_USAGE_COST_LABEL'] = 'Total'
result = {'type': 'string', 'value': f"Filtered Data: {filtered_df.to_dict(orient='records')}"}

The error could indeed be related to how filtered_df.to_dict(orient='records') is being processed, particularly if there’s a datetime field in the DataFrame that requires conversion to a string format. If strftime is invoked during this conversion and encounters an unexpected format or an issue with the datetime object, it could trigger the KeyError: 'import'.

This is happening multiple times.

rakendd avatar Aug 29 '24 04:08 rakendd