| import pandas as pd |
| from typing import List, Dict, Any |
|
|
| class DataLoader: |
| @staticmethod |
| def load_bond_data(csv_path: str) -> pd.DataFrame: |
| """ |
| Load bond data from a CSV file |
| |
| Args: |
| csv_path: Path to the CSV file containing bond data |
| |
| Returns: |
| pd.DataFrame: DataFrame containing the bond data |
| """ |
| try: |
| df = pd.read_csv(csv_path) |
| required_columns = ['ISIN', 'Maturity_Date', 'Coupon_Rate', 'Market_Yield'] |
| |
| |
| missing_columns = [col for col in required_columns if col not in df.columns] |
| if missing_columns: |
| raise ValueError(f"Missing required columns: {missing_columns}") |
| |
| return df |
| except Exception as e: |
| raise Exception(f"Error loading bond data: {str(e)}") |
|
|
| @staticmethod |
| def validate_data(df: pd.DataFrame) -> List[Dict[str, Any]]: |
| """ |
| Validate the bond data and return any validation errors |
| |
| Args: |
| df: DataFrame containing the bond data |
| |
| Returns: |
| List[Dict]: List of validation errors with row numbers and error messages |
| """ |
| validation_errors = [] |
| |
| for index, row in df.iterrows(): |
| |
| for column in df.columns: |
| if pd.isna(row[column]): |
| validation_errors.append({ |
| 'row': index + 2, |
| 'column': column, |
| 'error': 'Missing value' |
| }) |
| |
| |
| if not pd.isna(row['Coupon_Rate']): |
| try: |
| rate = float(row['Coupon_Rate']) |
| if not (0 <= rate <= 100): |
| validation_errors.append({ |
| 'row': index + 2, |
| 'column': 'Coupon_Rate', |
| 'error': 'Coupon rate must be between 0 and 100' |
| }) |
| except ValueError: |
| validation_errors.append({ |
| 'row': index + 2, |
| 'column': 'Coupon_Rate', |
| 'error': 'Invalid numeric value' |
| }) |
| |
| if not pd.isna(row['Market_Yield']): |
| try: |
| rate = float(row['Market_Yield']) |
| if not (0 <= rate <= 100): |
| validation_errors.append({ |
| 'row': index + 2, |
| 'column': 'Market_Yield', |
| 'error': 'Market yield must be between 0 and 100' |
| }) |
| except ValueError: |
| validation_errors.append({ |
| 'row': index + 2, |
| 'column': 'Market_Yield', |
| 'error': 'Invalid numeric value' |
| }) |
| |
| return validation_errors |
|
|