Update src/agents/agents.py
Browse filesMade changes to the planner_preprocessing_agent
185 unmodified lines186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267185 unmodified linesYou 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.### 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.### 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.```pythondf_cleaned = df.copy()```2. **Identify and separate columns**:* `numeric_columns`: Columns with numerical data.* `categorical_columns`: Columns with categorical data.3. **Handle missing values**:* **Numeric columns**: Fill missing values with **median**, **mean**, or another appropriate method.* **Categorical columns**: Fill with **mode**, **'Unknown'**, or another default value if appropriate.Example:```pythondf_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-based date columns to datetime**:Use the provided safe conversion method for date columns.```pythondef safe_to_datetime(date):try:return pd.to_datetime(date, errors='coerce', cache=False)except (ValueError, TypeError):return pd.NaTdf_cleaned['datetime_column'] = df_cleaned['datetime_column'].apply(safe_to_datetime)```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.7. **Do not mutate global state**: Avoid in-place modifications unless clearly necessary (e.g., using `.copy()`).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.### 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).### 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."""dataset = dspy.InputField(desc="The dataset, preloaded as df")185 unmodified lines186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270185 unmodified linesYou 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.* 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.### Best Practices for Preprocessing:1. Create a copy of the original DataFrame : It will always be stored as df, it already exists use it!```pythonprocessed_df = df.copy()```2. Separate column types :```pythonnumeric_cols = processed_df.select_dtypes(include='number').columnscategorical_cols = processed_df.select_dtypes(include='object').columns```3. Handle missing values :```pythonfor 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')```4. Convert string columns to datetime safely :```pythondef safe_to_datetime(x):try:return pd.to_datetime(x, errors='coerce', cache=False)except (ValueError, TypeError):return pd.NaTcleaned_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.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.7. Do not mutate global state : Avoid in-place modifications unless clearly necessary (e.g., using `.copy()`).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.### 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).### 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."""dataset = dspy.InputField(desc="The dataset, preloaded as df")
src/agents/agents.py+49-46