fill_missing_values
Fill or remove missing values in CSV data using strategies like dropping rows or specifying replacement values to clean datasets.
Instructions
Fill or remove missing values.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| session_id | Yes | ||
| strategy | No | drop | |
| value | No | ||
| columns | No |
Implementation Reference
- Core implementation of the fill_missing_values tool. Handles missing value strategies including drop, fillna with value/mean/median/mode/ffill/bfill using pandas DataFrame methods on the session data.async def fill_missing_values( session_id: str, strategy: str = "drop", value: Any = None, columns: Optional[List[str]] = None, ctx: Context = None ) -> Dict[str, Any]: """ Fill or remove missing values. Args: session_id: Session identifier strategy: One of 'drop', 'fill', 'forward', 'backward', 'mean', 'median', 'mode' value: Value to fill with (for 'fill' strategy) columns: Specific columns to apply to (None for all) ctx: FastMCP context Returns: Dict with success status and fill info """ try: manager = get_session_manager() session = manager.get_session(session_id) if not session or session.df is None: return {"success": False, "error": "Invalid session or no data loaded"} df = session.df null_counts_before = df.isnull().sum().to_dict() if columns: missing_cols = [col for col in columns if col not in df.columns] if missing_cols: return {"success": False, "error": f"Columns not found: {missing_cols}"} target_cols = columns else: target_cols = df.columns.tolist() if strategy == "drop": session.df = df.dropna(subset=target_cols) elif strategy == "fill": if value is None: return {"success": False, "error": "Value required for 'fill' strategy"} session.df[target_cols] = df[target_cols].fillna(value) elif strategy == "forward": session.df[target_cols] = df[target_cols].fillna(method='ffill') elif strategy == "backward": session.df[target_cols] = df[target_cols].fillna(method='bfill') elif strategy == "mean": for col in target_cols: if df[col].dtype in ['int64', 'float64']: session.df[col] = df[col].fillna(df[col].mean()) elif strategy == "median": for col in target_cols: if df[col].dtype in ['int64', 'float64']: session.df[col] = df[col].fillna(df[col].median()) elif strategy == "mode": for col in target_cols: mode_val = df[col].mode() if len(mode_val) > 0: session.df[col] = df[col].fillna(mode_val[0]) else: return {"success": False, "error": f"Unknown strategy: {strategy}"} null_counts_after = session.df.isnull().sum().to_dict() session.record_operation(OperationType.FILL_MISSING, { "strategy": strategy, "value": str(value) if value is not None else None, "columns": target_cols }) return { "success": True, "strategy": strategy, "rows_before": len(df), "rows_after": len(session.df), "null_counts_before": null_counts_before, "null_counts_after": null_counts_after } except Exception as e: logger.error(f"Error filling missing values: {str(e)}") return {"success": False, "error": str(e)}
- src/csv_editor/server.py:269-278 (registration)MCP tool registration via @mcp.tool decorator. Defines the tool interface and delegates execution to the core handler in transformations.py.@mcp.tool async def fill_missing_values( session_id: str, strategy: str = "drop", value: Any = None, columns: Optional[List[str]] = None, ctx: Context = None ) -> Dict[str, Any]: """Fill or remove missing values.""" return await _fill_missing_values(session_id, strategy, value, columns, ctx)
- src/csv_editor/server.py:188-199 (helper)Import statement that brings in the fill_missing_values implementation from transformations.py, aliased for use in server wrappers.from .tools.transformations import ( filter_rows as _filter_rows, sort_data as _sort_data, select_columns as _select_columns, rename_columns as _rename_columns, add_column as _add_column, remove_columns as _remove_columns, change_column_type as _change_column_type, fill_missing_values as _fill_missing_values, remove_duplicates as _remove_duplicates, update_column as _update_column )