-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathverifiers.py
230 lines (203 loc) · 8.21 KB
/
verifiers.py
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
"""This module provides the basic objects for the dataframe_validation"""
from dataclasses import dataclass
import pandas as pd
@dataclass
class StandardVerifier:
"""
The DataVerifier class provides a way to verify constraints on a
dataframe.
"""
data: pd.DataFrame
constraints: dict
enforce_dtypes: bool = False
def __post_init__(self):
"Post init calculations."
self.failed_rows = []
self.validation_summary = self.__validate_data()
self.validation_data: pd.DataFrame = self.__get_validation_data()
def check_data_type(self, constraint: str, col: str) -> bool:
"""Check data type against constraint"""
return self.data[col].dtype.name != constraint
def check_nullable(self, constraint: bool, col: str) -> int:
"""Check null values against constraint"""
if not constraint:
breaks = self.data[col].isna()
rows = self.data.loc[breaks].copy()
rows["Validation"] = f"nullable: {col}"
self.failed_rows.append(rows)
else:
breaks = pd.Series(False)
return breaks.sum()
def check_unique(self, constraint: bool, col: str) -> int:
"""Check duplicate values against constraint"""
if constraint:
breaks = (self.data[col].notna()) & (self.data[col].duplicated())
rows = self.data.loc[breaks].copy()
rows["Validation"] = f"unique: {col}"
self.failed_rows.append(rows)
else:
breaks = pd.Series(False)
return breaks.sum()
def check_max_length(self, constraint: int, col: str) -> int:
"""Check max length against constraint"""
if not pd.api.types.is_numeric_dtype(self.data[col]):
breaks = (self.data[col].notna()) & (
self.data[col].astype(str).str.len() > constraint
)
rows = self.data.loc[breaks].copy()
rows["Validation"] = f"max_length: {col}"
self.failed_rows.append(rows)
return breaks.sum()
return None
def check_min_length(self, constraint: int, col: str) -> int:
"""Check min length against constraint"""
if not pd.api.types.is_numeric_dtype(self.data[col]):
breaks = (self.data[col].notna()) & (
self.data[col].astype(str).str.len() < constraint
)
rows = self.data.loc[breaks].copy()
rows["Validation"] = f"min_legth:{col}"
self.failed_rows.append(rows)
return breaks.sum()
return None
def check_value_range(self, constraint: list, col: str) -> int:
"""Check range of values against constraint"""
breaks = (self.data[col].notnull()) & (
~self.data[col].isin(constraint)
)
rows = self.data.loc[breaks].copy()
rows["Validation"] = f"value_range: {col}"
self.failed_rows.append(rows)
return breaks.sum()
def check_max_value(self, constraint: str, col: str):
"""Check max value against constraint"""
breaks = self.data[col] > constraint
rows = self.data.loc[breaks].copy()
rows["Validation"] = f"max_value: {col}"
self.failed_rows.append(rows)
return breaks.sum()
def check_min_value(self, constraint: str, col: str):
"""Check min value against constraint"""
breaks = self.data[col] < constraint
rows = self.data.loc[breaks].copy()
rows["Validation"] = f"max_value: {col}"
self.failed_rows.append(rows)
return breaks.sum()
def check_min_date(self, constraint: str, col: str) -> int:
"""Check min date against constraint"""
if pd.api.types.is_datetime64_dtype(self.data[col]):
breaks = pd.to_datetime(
pd.Series(self.data[col], dtype="datetime64[ns]")
) < pd.to_datetime(constraint)
rows = self.data.loc[breaks].copy()
rows["Validation"] = f"min_date: {col}"
self.failed_rows.append(rows)
return breaks.sum()
return None
def check_max_date(self, constraint: str, col: str) -> int:
"""Check max date against constraint"""
if pd.api.types.is_datetime64_dtype(self.data[col]):
breaks = pd.to_datetime(
pd.Series(self.data[col], dtype="datetime64[ns]")
) > pd.to_datetime(constraint)
rows = self.data.loc[breaks].copy()
rows["Validation"] = f"max_date: {col}"
self.failed_rows.append(rows)
return breaks.sum()
return None
def _call_checks(self, check: str) -> dict:
"""
Map constraint names with functions.
:param check: a str of check type
:return: a dict of calculated constraints
"""
checks_dict = {
"data_type": self.check_data_type,
"nullable": self.check_nullable,
"unique": self.check_unique,
"max_length": self.check_max_length,
"min_length": self.check_min_length,
"value_range": self.check_value_range,
"max_value": self.check_max_value,
"min_value": self.check_min_value,
"max_date": self.check_max_date,
"min_date": self.check_min_date,
}
return checks_dict[check]
def __validate_data(self) -> pd.DataFrame:
"""
Run all checks for the DataFrame
:param enforce_dtypes: a bool to enforce constraint dtype on validation
:return: a DataFrame with number of breaks per column
"""
if self.enforce_dtypes:
dtypes = {
out_key: in_val
for out_key, out_val in self.constraints.items()
for in_key, in_val in out_val.items()
if in_key == "data_type"
}
self.data = self.data.astype(dtypes)
verification = {}
for col_index, value in self.constraints.items():
verification[col_index] = {
check_key: self._call_checks(check_key)(
self.constraints[col_index][check_key], col_index
)
for check_key, check_value in value.items()
}
return pd.DataFrame(verification)
def __get_validation_data(self) -> pd.DataFrame:
"""
Gets all DataFrame rows with validation breaks.
:param: None
:returns: a DataFrame with rows of validation breaks
"""
failed_data = pd.concat(self.failed_rows)
return failed_data
@dataclass
class CustomVerifier:
"""
The DataVerifier class provides a way to verify constraints on a
dataframe.
"""
data: pd.DataFrame
constraints: list
def __post_init__(self):
"Post init calculations."
self.failed_rows = []
self.validation_summary = self.__validate_data()
self.validation_data: pd.DataFrame = self.__get_validation_data()
def check_custom_constraints(self, constraint: dict) -> dict:
"""
Check custom constraints
:param constraint: a custom constraint dict with name and query keys
:return: an int with count of breaks
"""
rows = self.data.query(constraint["query"], engine="python").copy()
rows["Validation"] = f"{constraint['name']}: {constraint['query']}"
self.failed_rows.append(rows)
return rows.shape[0]
def __validate_data(self) -> pd.DataFrame:
"""
Run all checks for the dataframe
:param: None
:returns: a DataFrame with number of breaks per column
"""
verification = {}
for constraint in self.constraints:
verification[constraint["name"]] = {
"name": constraint["name"],
"rule": constraint["query"],
"count": self.check_custom_constraints(constraint)
}
summary = pd.DataFrame(verification).T.reset_index()
return summary[["name", "rule", "count"]]
def __get_validation_data(self) -> pd.DataFrame:
"""
Gets all dataframe rows with validation breaks.
:param None:
:returns: a DataFrame with rows of validation breaks
"""
failed_data = pd.concat(self.failed_rows)
return failed_data