File size: 3,271 Bytes
f993922
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
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']
            
            # Verify all required columns are present
            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():
            # Check for missing values
            for column in df.columns:
                if pd.isna(row[column]):
                    validation_errors.append({
                        'row': index + 2,  # Adding 2 to account for 0-based index and header row
                        'column': column,
                        'error': 'Missing value'
                    })
            
            # Check if coupon rate and market yield are numeric and within reasonable ranges
            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