|
| 1 | +import pandas as pd |
| 2 | +import numpy as np |
| 3 | + |
| 4 | + |
| 5 | +class EnrollmentsCleaning: |
| 6 | + def __init__(self, raw_data): |
| 7 | + self.raw_data = raw_data |
| 8 | + |
| 9 | + def __Drop_columns(self, df): |
| 10 | + """ |
| 11 | + Deletes the columns not needed for the analysis, |
| 12 | + if you want to add columns to delete change the const variable |
| 13 | + 'COLUMNS_TO_DROP'. |
| 14 | +
|
| 15 | + Args: |
| 16 | + df: pandas.DataFrame |
| 17 | +
|
| 18 | + Return: |
| 19 | + pandas.DataFrame |
| 20 | + """ |
| 21 | + COLUMNS_TO_DROP = ['Full Name'] |
| 22 | + result = df.drop(columns=COLUMNS_TO_DROP) |
| 23 | + return result |
| 24 | + |
| 25 | + def __Fix_nan_values(self, df): |
| 26 | + """ |
| 27 | + Gives values to NaN. |
| 28 | +
|
| 29 | + Args: |
| 30 | + df: pandas.DataFrame |
| 31 | +
|
| 32 | + Return: |
| 33 | + pandas.DataFrame |
| 34 | + """ |
| 35 | + # Fix NaN values |
| 36 | + NAN_VALUE_SUBSTITUTE = 'NA' |
| 37 | + columns_to_fix = { |
| 38 | + 'Projected Start Date': NAN_VALUE_SUBSTITUTE, |
| 39 | + 'Actual Start Date': NAN_VALUE_SUBSTITUTE, |
| 40 | + 'Projected End Date': NAN_VALUE_SUBSTITUTE, |
| 41 | + 'Actual End Date': NAN_VALUE_SUBSTITUTE, |
| 42 | + 'Outcome': NAN_VALUE_SUBSTITUTE |
| 43 | + } |
| 44 | + # 'ATP Cohort' NA will handle in a separed function |
| 45 | + for column, substitute_value in columns_to_fix.items(): |
| 46 | + df[column] = df[column].fillna(substitute_value) |
| 47 | + |
| 48 | + return df |
| 49 | + |
| 50 | + def __Rename_values(self, df): |
| 51 | + """ |
| 52 | + Changes values for consistency. |
| 53 | +
|
| 54 | + Args: |
| 55 | + df: pandas.DataFrame |
| 56 | +
|
| 57 | + Return: |
| 58 | + pandas.DataFrame |
| 59 | + """ |
| 60 | + df.loc[df['Service'] == 'Data Analytics 2', |
| 61 | + 'Service'] = 'Data Analysis 2' |
| 62 | + return df |
| 63 | + |
| 64 | + def __Delete_values(self, df): |
| 65 | + """ |
| 66 | + Deletes values not needed, if you want to add values to delete |
| 67 | + change the const variable 'VALUES_NOT_NEEDED'. |
| 68 | +
|
| 69 | +
|
| 70 | + Args: |
| 71 | + df: pandas.DataFrame |
| 72 | +
|
| 73 | + Return: |
| 74 | + pandas.DataFrame |
| 75 | + """ |
| 76 | + # 'Referral to External Service', 'Supportive Services Referral', |
| 77 | + # are deleted because dont have a "Projected Start Date" |
| 78 | + VALUES_NOT_NEEDED = { |
| 79 | + 'Service': ['Software Development 1', |
| 80 | + 'Software Development 2', |
| 81 | + 'Web Development 1', 'Web Development 2', |
| 82 | + 'Data Analysis 1', 'Data Analysis 2', |
| 83 | + 'Referral to External Service', |
| 84 | + 'Supportive Services Referral'] |
| 85 | + } |
| 86 | + for column, value in VALUES_NOT_NEEDED.items(): |
| 87 | + df = df[~df[column].isin(value)] |
| 88 | + return df |
| 89 | + |
| 90 | + def __Set_data_types(self, df): |
| 91 | + """ |
| 92 | + Sets data type for each column. |
| 93 | +
|
| 94 | + Args: |
| 95 | + df: pandas.DataFrame |
| 96 | +
|
| 97 | + Return: |
| 98 | + pandas.DataFrame |
| 99 | + """ |
| 100 | + # DataTypes |
| 101 | + column_datatype: dict = {'Auto Id': str, 'KY Region': str, |
| 102 | + 'Assessment ID': str, 'EnrollmentId': str, |
| 103 | + 'Enrollment Service Name': str, |
| 104 | + 'Service': str, |
| 105 | + 'Projected Start Date': str, |
| 106 | + 'Actual Start Date': str, |
| 107 | + 'Projected End Date': str, |
| 108 | + 'Actual End Date': str, |
| 109 | + 'Outcome': str, |
| 110 | + 'ATP Cohort': 'datetime64[ns]'} |
| 111 | + # TODO: 'Projected Start Date', 'Actual Start Date', |
| 112 | + # 'Projected End Date','Actual End Date' are all datetime |
| 113 | + # types but have a value fix of NA |
| 114 | + |
| 115 | + for column, type in column_datatype.items(): |
| 116 | + df[column] = df[column].astype(type) |
| 117 | + return df |
| 118 | + |
| 119 | + def __Find_cohort(self, id: str, |
| 120 | + projected_start_date: str, |
| 121 | + cohort_to_find: str, |
| 122 | + df_to_clean: pd.DataFrame): |
| 123 | + """ |
| 124 | + Finds values for each NaN of 'ATP Cohort' column. |
| 125 | + This function was created with the idea of using |
| 126 | + pandas.DataFrame.apply(). |
| 127 | +
|
| 128 | +
|
| 129 | + Args: |
| 130 | + id: str |
| 131 | + projected_start_date: str |
| 132 | + cohort_to_find: str |
| 133 | + df_to_clean: pandas.DataFrame |
| 134 | +
|
| 135 | + Return: |
| 136 | + numpy.array |
| 137 | + """ |
| 138 | + # Q: What to do with Service: ['Referral to External Service', |
| 139 | + # 'Supportive Services Referral'] |
| 140 | + # TODO: Clean the NaTType before this function runs |
| 141 | + if pd.isna(cohort_to_find): |
| 142 | + student_df = df_to_clean[df_to_clean['Auto Id'] == id] |
| 143 | + # remove ATP Cohort NA values, it can be more than one |
| 144 | + student_df: pd.DataFrame = student_df[~student_df['ATP Cohort'] |
| 145 | + .isna()] |
| 146 | + cohorts_participaded = student_df['ATP Cohort'].astype( |
| 147 | + 'datetime64[ns]').unique() |
| 148 | + |
| 149 | + # print(cohorts_participaded) |
| 150 | + if len(cohorts_participaded) == 1: |
| 151 | + return cohorts_participaded[0] |
| 152 | + else: |
| 153 | + # cohorts_participaded.append(pd.to_datetime(projected_start_date)) |
| 154 | + stimated_module_date = np.datetime64(projected_start_date) |
| 155 | + cohorts_participaded = np.append( |
| 156 | + cohorts_participaded, stimated_module_date) |
| 157 | + cohorts_participaded.sort() |
| 158 | + previus_date = cohorts_participaded[0] |
| 159 | + for cohort in cohorts_participaded: |
| 160 | + if stimated_module_date == cohort: |
| 161 | + return previus_date |
| 162 | + else: |
| 163 | + return np.datetime64(cohort_to_find) |
| 164 | + |
| 165 | + def Get_clean_data(self): |
| 166 | + """ |
| 167 | + Cleans the raw data. |
| 168 | +
|
| 169 | + Args: |
| 170 | + df: pandas.DataFrame |
| 171 | +
|
| 172 | + Return: |
| 173 | + pandas.DataFrame |
| 174 | + """ |
| 175 | + df = self.raw_data |
| 176 | + df = self.__Drop_columns(df) |
| 177 | + df = self.__Fix_nan_values(df) |
| 178 | + df = self.__Rename_values(df) |
| 179 | + df = self.__Delete_values(df) |
| 180 | + df = self.__Set_data_types(df) |
| 181 | + df['ATP Cohort'] = df.apply(lambda row: self.__Find_cohort( |
| 182 | + row['Auto Id'], |
| 183 | + row['Projected Start Date'], |
| 184 | + row['ATP Cohort'], |
| 185 | + df), axis=1) |
| 186 | + return df |
0 commit comments