1+ import pandas as pd
2+ import numpy as np
3+
4+ class EnrollmentsCleaning :
5+ def __init__ (self , raw_data ):
6+ self .raw_data = raw_data
7+
8+ def Drop_columns (self , df ):
9+ COLUMNS_TO_DROP = ['Full Name' ]
10+ result = df .drop (columns = COLUMNS_TO_DROP )
11+ return result
12+
13+ def Fix_nan_values (self , df ):
14+ # Fix NaN values
15+ NAN_VALUE_SUBSTITUTE = 'NA'
16+ columns_to_fix = {
17+ 'Projected Start Date' : NAN_VALUE_SUBSTITUTE , 'Actual Start Date' : NAN_VALUE_SUBSTITUTE , 'Projected End Date' : NAN_VALUE_SUBSTITUTE ,
18+ 'Actual End Date' : NAN_VALUE_SUBSTITUTE , 'Outcome' : NAN_VALUE_SUBSTITUTE
19+ }
20+ # 'ATP Cohort' NA will handle in a separed function
21+ for column , substitute_value in columns_to_fix .items ():
22+ df [column ] = df [column ].fillna (substitute_value )
23+
24+ return df
25+
26+ def Rename_values (self , df ):
27+ # Fix change name Data Analitics 2 to Data Analysis 2 for consistency
28+ df .loc [df ['Service' ] == 'Data Analytics 2' , 'Service' ] = 'Data Analysis 2'
29+ return df
30+
31+ def Delete_values (self , df ):
32+ # Delete values not needed
33+ # 'Referral to External Service', 'Supportive Services Referral', are deleted because dont have a "Projected Start Date"
34+ values_not_needed = {
35+ 'Service' : ['Software Development 1' , 'Software Development 2' , 'Web Development 1' , 'Web Development 2' , 'Data Analysis 1' ,'Data Analysis 2' , 'Referral to External Service' , 'Supportive Services Referral' ]
36+ }
37+ for column , value in values_not_needed .items ():
38+ df = df [~ df [column ].isin (value )]
39+ return df
40+
41+ def Set_data_types (self , df ):
42+ # DataTypes
43+ column_datatype : dict = {'Auto Id' : str , 'KY Region' : str , 'Assessment ID' : str , 'EnrollmentId' : str ,
44+ 'Enrollment Service Name' : str , 'Service' : str , 'Projected Start Date' : str ,
45+ 'Actual Start Date' : str , 'Projected End Date' : str , 'Actual End Date' : str , 'Outcome' : str ,
46+ 'ATP Cohort' : 'datetime64[ns]' }
47+ # TODO: 'Projected Start Date', 'Actual Start Date', 'Projected End Date', 'Actual End Date' are all datetime types but have a value fix of NA
48+
49+ for column , type in column_datatype .items ():
50+ df [column ] = df [column ].astype (type )
51+ return df
52+
53+ def Find_cohort (self , id : str , projected_start_date : str , cohort_to_find : str , df_to_clean : pd .DataFrame ):
54+ ## Q: What to do with Service: ['Referral to External Service', 'Supportive Services Referral']
55+ ## TODO: Clean the NaTType before this function runs
56+ if pd .isna (cohort_to_find ):
57+ student_df = df_to_clean [df_to_clean ['Auto Id' ] == id ]
58+ # remove ATP Cohort NA values, it can be more than one
59+ student_df : pd .DataFrame = student_df [~ student_df ['ATP Cohort' ].isna ()]
60+ cohorts_participaded = student_df ['ATP Cohort' ].astype ('datetime64[ns]' ).unique ()
61+
62+ # print(cohorts_participaded)
63+ if len (cohorts_participaded ) == 1 :
64+ return cohorts_participaded [0 ]
65+ else :
66+ # cohorts_participaded.append(pd.to_datetime(projected_start_date))
67+ stimated_module_date = np .datetime64 (projected_start_date )
68+ cohorts_participaded = np .append (cohorts_participaded , stimated_module_date )
69+ cohorts_participaded .sort ()
70+ previus_date = cohorts_participaded [0 ]
71+ for cohort in cohorts_participaded :
72+ if stimated_module_date == cohort :
73+ return previus_date
74+ else :
75+ return np .datetime64 (cohort_to_find )
76+
77+ def Get_clean_data (self ):
78+ df = self .raw_data
79+ df = self .Drop_columns (df )
80+ df = self .Fix_nan_values (df )
81+ df = self .Rename_values (df )
82+ df = self .Delete_values (df )
83+ df = self .Set_data_types (df )
84+ df ['ATP Cohort' ] = df .apply (lambda row : self .Find_cohort (row ['Auto Id' ], row ['Projected Start Date' ], row ['ATP Cohort' ], df ), axis = 1 )
85+ return df
0 commit comments