File size: 2,132 Bytes
cda703b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import pandas as pd 
import os
import streamlit as st


@st.cache_resource(show_spinner=False)

def load_data():
    """

    Loads the optimized parquet file from the data directory.

    Includes graceful error handling for missing files.

    """
    file_path = os.path.join("data","loan_cleaned.parquet")
    drop_cols = [ #reducing noise
        'id', 'member_id', 'url', 'desc', 'emp_title', 'title', 'zip_code', 
        'policy_code', 'pymnt_plan', 'application_type', 'initial_list_status',
        'funded_amnt', 'funded_amnt_inv', 'out_prncp', 'out_prncp_inv', 
        'total_pymnt', 'total_pymnt_inv', 'total_rec_prncp', 'total_rec_int',
        'total_rec_late_fee', 'recoveries', 'collection_recovery_fee',
        'last_pymnt_d', 'last_pymnt_amnt', 'next_pymnt_d', 'last_credit_pull_d'
    ]
    
    try:
        df = pd.read_parquet(file_path,engine='pyarrow')
        """

        Calculates the global baseline default rate of the portfolio.

        This acts as the 'Visual Anchor' for all our risk assessments.

        """
        df.drop(columns=drop_cols, errors='ignore', inplace=True)

        #creating bad_loan column for statistical analysis
        bad_statuses = ['Charged Off',
                'Late (31-120 days)',
                'In Grace Period',
                'Late (16-30 days)',
                'Does not meet the credit policy. Status:Charged Off',
                'Default']
        df['bad_loan'] = df['loan_status'].apply(lambda x:1 if x in bad_statuses else 0)

        for col in df.select_dtypes(include=['object', 'string']).columns:
            df[col] = df[col].astype('category')
        
    
        float_cols = df.select_dtypes(include=['float64']).columns
        df[float_cols] = df[float_cols].astype('float32')
    
    
        int_cols = df.select_dtypes(include=['int64']).columns
        df[int_cols] = df[int_cols].astype('int32')
    
    
        return df
    except FileNotFoundError:
        st.error(f"Critical Error: The Dataset was not found at {file_path}. Please check your pipeline")
        st.stop()