-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvalidation.py
More file actions
202 lines (176 loc) · 8.47 KB
/
Copy pathvalidation.py
File metadata and controls
202 lines (176 loc) · 8.47 KB
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
import pandas as pd
import numpy as np
from validate_email import validate_email
class Validation():
def __init__(self) -> None:
pass
def validate_empty(self, dataFrame, col, sId, status_df, filename='Unknown_file'):
'''
check for empty value in the dataframe with column
'''
try:
df = dataFrame[dataFrame[col].isnull()]
row_status = {}
# print(df)
if not df.empty:
for index, row in df.iterrows():
desc = 'FileName_'+ filename + ' RowNumber_' + str(index)
row_status = {
'id': row[sId], 'action': 'Rejected - Empty', 'description': desc}
status_df = status_df.append(row_status, ignore_index=True)
print(f"validate_empty_values run Succesfully!!")
dataFrame = dataFrame[np.invert(dataFrame[col].isnull())]
return status_df, dataFrame
print(f"validate_empty run Succesfully!!")
return status_df, dataFrame
except Exception as e:
print(f"Error in the validate_empty")
print(f'Error - {e}')
def validate_non_unique(self, dataFrame, col, sId, status_df, filename='Unknown_file'):
'''
check for empty value in the dataframe with column
'''
try:
df = dataFrame[dataFrame.duplicated(subset=[col])]
row_status = {}
# print(df)
if not df.empty:
for index, row in df.iterrows():
desc = 'FileName_'+filename + '_RowNumber_' + \
str(index) + '_Column_' + col
row_status = {
'id': row[sId], 'action': 'Rejected - NOT UNIQUE', 'description': desc}
status_df = status_df.append(
row_status, ignore_index=True)
dataFrame = dataFrame[np.invert(
dataFrame.duplicated(subset=[col]))]
return status_df, dataFrame
return status_df, dataFrame
except Exception as e:
print(f"Error in the validate_non_unique")
print(f'Error - {e}')
def validate_email(self, dataFrame, col, sId, status_df, filename='Unknown_file'):
'''
check for empty value in the dataframe with column
'''
try:
df = dataFrame[np.invert(dataFrame[col].apply(validate_email))]
row_status = {}
# print(df)
if not df.empty:
for index, row in df.iterrows():
desc = 'FileName_'+filename + '_RowNumber_' + \
str(index) + '_Column_' + col
row_status = {
'id': row[sId], 'action': 'Rejected - NOT UNIQUE', 'description': desc}
status_df = status_df.append(
row_status, ignore_index=True)
dataFrame = dataFrame[dataFrame[col].apply(validate_email)]
return status_df, dataFrame
return status_df, dataFrame
except Exception as e:
print(f"Error in the validate_email")
print(f'Error - {e}')
def validate_orgs_id(self, dataFrame, orgs_df, col, sId, status_df, filename='Unknown_file'):
'''
check for empty value in the dataframe with column
'''
try:
if str(orgs_df['sourcedId'].dtype) == "object":
orgs_df['sourcedId'] = orgs_df['sourcedId'].apply(
lambda x: int(x.strip("'")))
if str(dataFrame[col].dtype) == "object":
dataFrame[col] = dataFrame[col].apply(
lambda x: int(x.strip("'")))
df = dataFrame[np.invert(
dataFrame[col].isin(orgs_df['sourcedId']))]
row_status = {}
if not df.empty:
for index, row in df.iterrows():
desc = 'FileName_'+filename + '_RowNumber_' + \
str(index) + '_Column_' + col
row_status = {
'id': row[sId], 'action': 'Rejected - INVALID ORG REF', 'description': desc}
status_df = status_df.append(
row_status, ignore_index=True)
dataFrame = dataFrame[dataFrame[col].isin(orgs_df['sourcedId'])]
return status_df, dataFrame
return status_df, dataFrame
except Exception as e:
print(f"Error in the validate_orgs_id")
print(f'Error - {e}')
def validate_course_credit(self, dataFrame, col, sId, status_df, filename='Unknown_file'):
'''
check for empty value in the dataframe with column
'''
try:
df_fixed = dataFrame[dataFrame[col].str.contains("'", regex=False)]
for index, row in df_fixed.iterrows():
desc = 'FileName_'+filename + '_RowNumber_' + \
str(index) + '_Column_' + col
row_status = {
'id': row[sId], 'action': 'Fixed - removed Quotes', 'description': desc}
status_df = status_df.append(row_status, ignore_index=True)
if str(dataFrame[col].dtype) == "object":
dataFrame[col] = dataFrame[col].apply(lambda x: x.strip("'"))
#print(dataFrame[col])
for index, row in dataFrame.iterrows():
try:
dataFrame[col][index] = float(dataFrame[col][index])
except Exception as e:
print(f"Error in cleaning needs to rejected - {e}")
desc = 'FileName_'+filename + '_RowNumber_' + \
str(index) + '_Column_' + col
row_status = {
'id': row[sId], 'action': 'Rejected - INVALID CREDIT', 'description': desc}
status_df = status_df.append(row_status, ignore_index=True)
dataFrame = dataFrame.drop(index)
return status_df, dataFrame
except Exception as e:
print(f"Error in the validate_course_credit")
print(f'Error - {e}')
def normalize_validate_date(self, dataFrame, col, sId, status_df, filename='Unknown_file'):
'''
check for valid dates in the dataframe on date
'''
try:
df = dataFrame.copy(deep=True)
dataFrame[col] = pd.to_datetime(dataFrame[col], errors='coerce')
dataFrame[col] = dataFrame[col].dt.date
row_status = {}
df_rejected = df[dataFrame[col].isnull()]
dataFrame = dataFrame[np.invert(dataFrame[col].isnull())]
if not df_rejected.empty:
for index, row in df_rejected.iterrows():
desc = 'FileName_'+filename + ' RowNumber_' + str(index)
row_status = {
'id': row[sId], 'action': 'Rejected - INVALID DATE', 'description': desc}
status_df = status_df.append(
row_status, ignore_index=True)
return status_df, dataFrame
return status_df, dataFrame
except Exception as e:
print(f"Error in the normalize_validate_date")
print(f'Error - {e}')
def validate_date(self, dataFrame, start, end, sId, status_df, filename='Unknown_file'):
'''
check for valid dates in the dataframe on date
'''
try:
df = dataFrame.copy(deep=True)
date_df = (dataFrame[end] - dataFrame[start]).astype(int)
df_rejected = dataFrame[date_df<0]
row_status = {}
if not df_rejected.empty:
for index, row in df_rejected.iterrows():
desc = 'FileName_'+filename + ' RowNumber_' + str(index) + 'start date is greater than end date'
row_status = {
'id': row[sId], 'action': 'Rejected_invalid_Date', 'description': desc}
status_df = status_df.append(
row_status, ignore_index=True)
dataFrame = dataFrame[date_df>0]
return status_df, dataFrame
return status_df, dataFrame
except Exception as e:
print(f"Error in the validate_date")
print(f'Error - {e}')