Update src/agents/agents.py
Browse filesMade changes to the planner_preprocessing_agent
185 unmodified lines186187188189190191189190191192193194195195196197198196197198199200201202202203203205206207204205209206207208209211212210211214212213214215216216217217218219219220221222223224225226221222223224227228226227229230231232233234235236237238239240229230231232233234235236241238242243244241245246243247248245249250251252253250254255256257254255258259260261262259260261262263264263264265266267268269270185 unmodified lines
You are given:
* A **dataset** (already loaded as `df`).* A **user-defined analysis goal** (e.g., predictive modeling, exploration, cleaning).* **Agent-specific plan instructions** that tell you what variables you are expected to **create** and what variables you are **receiving** from previous agents.* A dataset (already loaded as `df`).* A user-defined analysis goal (e.g., predictive modeling, exploration, cleaning).* Agent-specific plan instructions that tell you what variables you are expected to create and what variables you are receiving from previous agents.* processed_df is just an arbitrary name, it can be anything the planner says to clean!
### Your Responsibilities:
* **Follow the provided plan** and create only the required variables listed in the 'create' section of the plan instructions.* **Do not create fake data** or introduce variables not explicitly part of the instructions.* **Do not read data from CSV**; the dataset (`df`) is already loaded and ready for processing.* Generate Python code using **NumPy** and **Pandas** to preprocess the data and produce any intermediate variables as specified in the plan instructions.* Follow the provided plan and create only the required variables listed in the 'create' section of the plan instructions.* Do not create fake data or introduce variables not explicitly part of the instructions.* Do not read data from CSV ; the dataset (`df`) is already loaded and ready for processing.* Generate Python code using NumPy and Pandas to preprocess the data and produce any intermediate variables as specified in the plan instructions.
### Best Practices for Preprocessing:
1. **Create a copy of the DataFrame**: Always work with a copy of the original dataset to avoid modifying it directly.
```python df_cleaned = df.copy() ```1. Create a copy of the original DataFrame : It will always be stored as df, it already exists use it!
2. **Identify and separate columns**: ```python processed_df = df.copy() ```
* `numeric_columns`: Columns with numerical data. * `categorical_columns`: Columns with categorical data.2. Separate column types :
3. **Handle missing values**: ```python numeric_cols = processed_df.select_dtypes(include='number').columns categorical_cols = processed_df.select_dtypes(include='object').columns ```
* **Numeric columns**: Fill missing values with **median**, **mean**, or another appropriate method. * **Categorical columns**: Fill with **mode**, **'Unknown'**, or another default value if appropriate.3. Handle missing values :
Example: ```python for col in numeric_cols: processed_df[col] = processed_df[col].fillna(processed_df[col].median()) for col in categorical_cols: processed_df[col] = processed_df[col].fillna(processed_df[col].mode()[0] if not processed_df[col].mode().empty else 'Unknown') ```
```python df_cleaned['numeric_column'] = df_cleaned['numeric_column'].fillna(df_cleaned['numeric_column'].median()) df_cleaned['categorical_column'] = df_cleaned['categorical_column'].fillna(df_cleaned['categorical_column'].mode()[0]) ```4. Convert string columns to datetime safely :
4. **Convert string-based date columns to datetime**: Use the provided safe conversion method for date columns. ```python def safe_to_datetime(x): try: return pd.to_datetime(x, errors='coerce', cache=False) except (ValueError, TypeError): return pd.NaT cleaned_df['date_column'] = cleaned_df['date_column'].apply(safe_to_datetime) ```
> Replace `processed_df`,'cleaned_df' and `date_column` with whatever names the user or planner provides.
```python def safe_to_datetime(date): try: return pd.to_datetime(date, errors='coerce', cache=False) except (ValueError, TypeError): return pd.NaT df_cleaned['datetime_column'] = df_cleaned['datetime_column'].apply(safe_to_datetime) ```
5. **Do not alter the DataFrame index**:5. Do not alter the DataFrame index : Avoid using `reset_index()`, `set_index()`, or reindexing unless explicitly instructed.
6. **Log assumptions and corrections** in comments to clarify any choices made during preprocessing.6. Log assumptions and corrections in comments to clarify any choices made during preprocessing.
7. **Do not mutate global state**: Avoid in-place modifications unless clearly necessary (e.g., using `.copy()`).7. Do not mutate global state : Avoid in-place modifications unless clearly necessary (e.g., using `.copy()`).
8. **Handle data types properly**:8. Handle data types properly :
* Avoid coercing types blindly (e.g., don't compare timestamps to strings or floats). * Use `pd.to_datetime(..., errors='coerce')` for safe datetime parsing.
9. **Preserve column structure**: Only drop or rename columns if explicitly instructed.9. Preserve column structure : Only drop or rename columns if explicitly instructed.
### Output:
1. **Code**: Python code that performs the requested preprocessing steps as per the plan instructions.2. **Summary**: A brief explanation of what preprocessing was done (e.g., columns handled, missing value treatment).1. Code : Python code that performs the requested preprocessing steps as per the plan instructions.2. Summary : A brief explanation of what preprocessing was done (e.g., columns handled, missing value treatment).
### Principles to Follow:
* **Never alter the DataFrame index** unless explicitly instructed.* **Handle missing data** explicitly, filling with default values when necessary.* **Preserve column structure** and avoid unnecessary modifications.* **Ensure data types are appropriate** (e.g., dates parsed correctly).* **Log assumptions** in the code.
-Never alter the DataFrame index unless explicitly instructed.-Handle missing data explicitly, filling with default values when necessary.-Preserve column structure and avoid unnecessary modifications.-Ensure data types are appropriate (e.g., dates parsed correctly).-Log assumptions in the code.
""" dataset = dspy.InputField(desc="The dataset, preloaded as df")src/agents/agents.py+49-46