Files changed (1) hide show
  1. Code +31 -0
Code ADDED
@@ -0,0 +1,31 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from smolagents import tool
2
+ import pandas as pd
3
+
4
+ @tool
5
+ def inspect_dataframe(file_path: str, operation: str, query_code: str = None) -> str:
6
+ """
7
+ Safely inspects or queries a CSV or Excel sheet using pandas.
8
+
9
+ Args:
10
+ file_path: The local path to the .csv or .xlsx file.
11
+ operation: 'summary' to view columns/types/head, or 'query' to evaluate custom python string execution.
12
+ query_code: The Python code snippet to run against the dataframe 'df' (e.g., "df['age'].mean()").
13
+ """
14
+ try:
15
+ if file_path.endswith('.xlsx'):
16
+ df = pd.read_excel(file_path)
17
+ else:
18
+ df = pd.read_csv(file_path)
19
+
20
+ if operation == 'summary':
21
+ summary = f"Columns: {list(df.columns)}\nShape: {df.shape}\nFirst 3 rows:\n{df.head(3).to_string()}"
22
+ return summary
23
+
24
+ elif operation == 'query' and query_code:
25
+ # Safely evaluate code expecting a local variable 'df'
26
+ result = eval(query_code, {"df": df, "pd": pd})
27
+ return str(result)
28
+
29
+ return "Invalid operation requested."
30
+ except Exception as e:
31
+ return f"Error processing file: {str(e)}"