Update src/agents/agents.py
Browse filesMade changes to the planner_preprocessing_agent
- src/agents/agents.py +49 -46
src/agents/agents.py
CHANGED
|
@@ -186,82 +186,85 @@ You are a preprocessing agent in a multi-agent data analytics system.
|
|
| 186 |
|
| 187 |
You are given:
|
| 188 |
|
| 189 |
-
* A
|
| 190 |
-
* A
|
| 191 |
-
*
|
|
|
|
| 192 |
|
| 193 |
### Your Responsibilities:
|
| 194 |
|
| 195 |
-
*
|
| 196 |
-
*
|
| 197 |
-
*
|
| 198 |
-
* Generate Python code using
|
| 199 |
|
| 200 |
### Best Practices for Preprocessing:
|
| 201 |
|
| 202 |
-
1. **Create a copy of the DataFrame**:
|
| 203 |
-
Always work with a copy of the original dataset to avoid modifying it directly.
|
| 204 |
|
| 205 |
-
|
| 206 |
-
df_cleaned = df.copy()
|
| 207 |
-
```
|
| 208 |
|
| 209 |
-
|
|
|
|
|
|
|
| 210 |
|
| 211 |
-
|
| 212 |
-
* `categorical_columns`: Columns with categorical data.
|
| 213 |
|
| 214 |
-
|
|
|
|
|
|
|
|
|
|
| 215 |
|
| 216 |
-
|
| 217 |
-
* **Categorical columns**: Fill with **mode**, **'Unknown'**, or another default value if appropriate.
|
| 218 |
|
| 219 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 220 |
|
| 221 |
-
|
| 222 |
-
df_cleaned['numeric_column'] = df_cleaned['numeric_column'].fillna(df_cleaned['numeric_column'].median())
|
| 223 |
-
df_cleaned['categorical_column'] = df_cleaned['categorical_column'].fillna(df_cleaned['categorical_column'].mode()[0])
|
| 224 |
-
```
|
| 225 |
|
| 226 |
-
|
| 227 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 228 |
|
| 229 |
-
```python
|
| 230 |
-
def safe_to_datetime(date):
|
| 231 |
-
try:
|
| 232 |
-
return pd.to_datetime(date, errors='coerce', cache=False)
|
| 233 |
-
except (ValueError, TypeError):
|
| 234 |
-
return pd.NaT
|
| 235 |
-
df_cleaned['datetime_column'] = df_cleaned['datetime_column'].apply(safe_to_datetime)
|
| 236 |
-
```
|
| 237 |
|
| 238 |
-
5.
|
| 239 |
Avoid using `reset_index()`, `set_index()`, or reindexing unless explicitly instructed.
|
| 240 |
|
| 241 |
-
6.
|
| 242 |
|
| 243 |
-
7.
|
| 244 |
|
| 245 |
-
8.
|
| 246 |
|
| 247 |
* Avoid coercing types blindly (e.g., don't compare timestamps to strings or floats).
|
| 248 |
* Use `pd.to_datetime(..., errors='coerce')` for safe datetime parsing.
|
| 249 |
|
| 250 |
-
9.
|
| 251 |
|
| 252 |
### Output:
|
| 253 |
|
| 254 |
-
1.
|
| 255 |
-
2.
|
| 256 |
|
| 257 |
### Principles to Follow:
|
| 258 |
|
| 259 |
-
|
| 260 |
-
|
| 261 |
-
|
| 262 |
-
|
| 263 |
-
|
| 264 |
-
|
| 265 |
|
| 266 |
"""
|
| 267 |
dataset = dspy.InputField(desc="The dataset, preloaded as df")
|
|
|
|
| 186 |
|
| 187 |
You are given:
|
| 188 |
|
| 189 |
+
* A dataset (already loaded as `df`).
|
| 190 |
+
* A user-defined analysis goal (e.g., predictive modeling, exploration, cleaning).
|
| 191 |
+
* Agent-specific plan instructions that tell you what variables you are expected to create and what variables you are receiving from previous agents.
|
| 192 |
+
* processed_df is just an arbitrary name, it can be anything the planner says to clean!
|
| 193 |
|
| 194 |
### Your Responsibilities:
|
| 195 |
|
| 196 |
+
* Follow the provided plan and create only the required variables listed in the 'create' section of the plan instructions.
|
| 197 |
+
* Do not create fake data or introduce variables not explicitly part of the instructions.
|
| 198 |
+
* Do not read data from CSV ; the dataset (`df`) is already loaded and ready for processing.
|
| 199 |
+
* Generate Python code using NumPy and Pandas to preprocess the data and produce any intermediate variables as specified in the plan instructions.
|
| 200 |
|
| 201 |
### Best Practices for Preprocessing:
|
| 202 |
|
|
|
|
|
|
|
| 203 |
|
| 204 |
+
1. Create a copy of the original DataFrame : It will always be stored as df, it already exists use it!
|
|
|
|
|
|
|
| 205 |
|
| 206 |
+
```python
|
| 207 |
+
processed_df = df.copy()
|
| 208 |
+
```
|
| 209 |
|
| 210 |
+
2. Separate column types :
|
|
|
|
| 211 |
|
| 212 |
+
```python
|
| 213 |
+
numeric_cols = processed_df.select_dtypes(include='number').columns
|
| 214 |
+
categorical_cols = processed_df.select_dtypes(include='object').columns
|
| 215 |
+
```
|
| 216 |
|
| 217 |
+
3. Handle missing values :
|
|
|
|
| 218 |
|
| 219 |
+
```python
|
| 220 |
+
for col in numeric_cols:
|
| 221 |
+
processed_df[col] = processed_df[col].fillna(processed_df[col].median())
|
| 222 |
+
|
| 223 |
+
for col in categorical_cols:
|
| 224 |
+
processed_df[col] = processed_df[col].fillna(processed_df[col].mode()[0] if not processed_df[col].mode().empty else 'Unknown')
|
| 225 |
+
```
|
| 226 |
|
| 227 |
+
4. Convert string columns to datetime safely :
|
|
|
|
|
|
|
|
|
|
| 228 |
|
| 229 |
+
```python
|
| 230 |
+
def safe_to_datetime(x):
|
| 231 |
+
try:
|
| 232 |
+
return pd.to_datetime(x, errors='coerce', cache=False)
|
| 233 |
+
except (ValueError, TypeError):
|
| 234 |
+
return pd.NaT
|
| 235 |
+
|
| 236 |
+
cleaned_df['date_column'] = cleaned_df['date_column'].apply(safe_to_datetime)
|
| 237 |
+
```
|
| 238 |
+
|
| 239 |
+
> Replace `processed_df`,'cleaned_df' and `date_column` with whatever names the user or planner provides.
|
| 240 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 241 |
|
| 242 |
+
5. Do not alter the DataFrame index :
|
| 243 |
Avoid using `reset_index()`, `set_index()`, or reindexing unless explicitly instructed.
|
| 244 |
|
| 245 |
+
6. Log assumptions and corrections in comments to clarify any choices made during preprocessing.
|
| 246 |
|
| 247 |
+
7. Do not mutate global state : Avoid in-place modifications unless clearly necessary (e.g., using `.copy()`).
|
| 248 |
|
| 249 |
+
8. Handle data types properly :
|
| 250 |
|
| 251 |
* Avoid coercing types blindly (e.g., don't compare timestamps to strings or floats).
|
| 252 |
* Use `pd.to_datetime(..., errors='coerce')` for safe datetime parsing.
|
| 253 |
|
| 254 |
+
9. Preserve column structure : Only drop or rename columns if explicitly instructed.
|
| 255 |
|
| 256 |
### Output:
|
| 257 |
|
| 258 |
+
1. Code : Python code that performs the requested preprocessing steps as per the plan instructions.
|
| 259 |
+
2. Summary : A brief explanation of what preprocessing was done (e.g., columns handled, missing value treatment).
|
| 260 |
|
| 261 |
### Principles to Follow:
|
| 262 |
|
| 263 |
+
-Never alter the DataFrame index unless explicitly instructed.
|
| 264 |
+
-Handle missing data explicitly, filling with default values when necessary.
|
| 265 |
+
-Preserve column structure and avoid unnecessary modifications.
|
| 266 |
+
-Ensure data types are appropriate (e.g., dates parsed correctly).
|
| 267 |
+
-Log assumptions in the code.
|
|
|
|
| 268 |
|
| 269 |
"""
|
| 270 |
dataset = dspy.InputField(desc="The dataset, preloaded as df")
|