-
Notifications
You must be signed in to change notification settings - Fork 36
/
validation.py
419 lines (328 loc) · 14 KB
/
validation.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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
import abc
import math
import datetime
import pandas as pd
import numpy as np
import typing
import operator
from . import column
from .validation_warning import ValidationWarning
from .errors import PanSchArgumentError
from pandas.api.types import is_categorical_dtype, is_numeric_dtype
class _BaseValidation:
"""
The validation base class that defines any object that can create a list of errors from a Series
"""
__metaclass__ = abc.ABCMeta
@abc.abstractmethod
def get_errors(self, series: pd.Series, column: 'column.Column') -> typing.Iterable[ValidationWarning]:
"""
Return a list of errors in the given series
:param series:
:param column:
:return:
"""
class _SeriesValidation(_BaseValidation):
"""
Implements the _BaseValidation interface by returning a Boolean series for each element that either passes or
fails the validation
"""
__metaclass__ = abc.ABCMeta
def __init__(self, **kwargs):
self._custom_message = kwargs.get('message')
@property
def message(self):
return self._custom_message or self.default_message
@abc.abstractproperty
def default_message(self) -> str:
"""
Create a message to be displayed whenever this validation fails
This should be a generic message for the validation type, but can be overwritten if the user provides a
message kwarg
"""
@abc.abstractmethod
def validate(self, series: pd.Series) -> pd.Series:
"""
Returns a Boolean series, where each value of False is an element in the Series that has failed the validation
:param series:
:return:
"""
def __invert__(self):
"""
Returns a negated version of this validation
"""
return _InverseValidation(self)
def __or__(self, other: '_SeriesValidation'):
"""
Returns a validation which is true if either this or the other validation is true
"""
return _CombinedValidation(self, other, operator.or_)
def __and__(self, other: '_SeriesValidation'):
"""
Returns a validation which is true if either this or the other validation is true
"""
return _CombinedValidation(self, other, operator.and_)
def get_errors(self, series: pd.Series, column: 'column.Column'):
errors = []
# Calculate which columns are valid using the child class's validate function, skipping empty entries if the
# column specifies to do so
simple_validation = ~self.validate(series)
if column.allow_empty:
# Failing results are those that are not empty, and fail the validation
# explicitly check to make sure the series isn't a category because issubdtype will FAIL if it is
if is_categorical_dtype(series) or is_numeric_dtype(series):
validated = ~series.isnull() & simple_validation
else:
validated = (series.str.len() > 0) & simple_validation
else:
validated = simple_validation
# Cut down the original series to only ones that failed the validation
indices = series.index[validated]
# Use these indices to find the failing items. Also print the index which is probably a row number
for i in indices:
element = series[i]
errors.append(ValidationWarning(
message=self.message,
value=element,
row=i,
column=series.name
))
return errors
class _InverseValidation(_SeriesValidation):
"""
Negates an ElementValidation
"""
def __init__(self, validation: _SeriesValidation):
self.negated = validation
super().__init__()
def validate(self, series: pd.Series):
return ~ self.negated.validate(series)
@property
def default_message(self):
return self.negated.message + ' <negated>'
class _CombinedValidation(_SeriesValidation):
"""
Validates if one and/or the other validation is true for an element
"""
def __init__(self, validation_a: _SeriesValidation, validation_b: _SeriesValidation, operator):
self.operator = operator
self.v_a = validation_a
self.v_b = validation_b
super().__init__()
def validate(self, series: pd.Series):
return self.operator(self.v_a.validate(series), self.v_b.validate(series))
@property
def default_message(self):
return '({}) {} ({})'.format(self.v_a.message, self.operator, self.v_b.message)
class CustomSeriesValidation(_SeriesValidation):
"""
Validates using a user-provided function that operates on an entire series (for example by using one of the pandas
Series methods: http://pandas.pydata.org/pandas-docs/stable/api.html#series)
"""
def __init__(self, validation: typing.Callable[[pd.Series], pd.Series], message: str):
"""
:param message: The error message to provide to the user if this validation fails. The row and column and
failing value will automatically be prepended to this message, so you only have to provide a message that
describes what went wrong, for example 'failed my validation' will become
{row: 1, column: "Column Name"}: "Value" failed my validation
:param validation: A function that takes a pandas Series and returns a boolean Series, where each cell is equal
to True if the object passed validation, and False if it failed
"""
self._validation = validation
super().__init__(message=message)
def validate(self, series: pd.Series) -> pd.Series:
return self._validation(series)
class CustomElementValidation(_SeriesValidation):
"""
Validates using a user-provided function that operates on each element
"""
def __init__(self, validation: typing.Callable[[typing.Any], typing.Any], message: str):
"""
:param message: The error message to provide to the user if this validation fails. The row and column and
failing value will automatically be prepended to this message, so you only have to provide a message that
describes what went wrong, for example 'failed my validation' will become
{row: 1, column: "Column Name"}: "Value" failed my validation
:param validation: A function that takes the value of a data frame cell and returns True if it passes the
the validation, and false if it doesn't
"""
self._validation = validation
super().__init__(message=message)
def validate(self, series: pd.Series) -> pd.Series:
return series.apply(self._validation)
class InRangeValidation(_SeriesValidation):
"""
Checks that each element in the series is within a given numerical range
"""
def __init__(self, min: float = -math.inf, max: float = math.inf, **kwargs):
"""
:param min: The minimum (inclusive) value to accept
:param max: The maximum (exclusive) value to accept
"""
self.min = min
self.max = max
super().__init__(**kwargs)
@property
def default_message(self):
return 'was not in the range [{}, {})'.format(self.min, self.max)
def validate(self, series: pd.Series) -> pd.Series:
series = pd.to_numeric(series, errors="coerce")
return (series >= self.min) & (series < self.max)
class IsDtypeValidation(_BaseValidation):
"""
Checks that a series has a certain numpy dtype
"""
def __init__(self, dtype: np.dtype, **kwargs):
"""
:param dtype: The numpy dtype to check the column against
"""
self.dtype = dtype
super().__init__(**kwargs)
def get_errors(self, series: pd.Series, column: 'column.Column' = None):
if not np.issubdtype(series.dtype, self.dtype):
return [ValidationWarning(
'The column {} has a dtype of {} which is not a subclass of the required type {}'.format(
column.name if column else '', series.dtype, self.dtype
)
)]
else:
return []
class CanCallValidation(_SeriesValidation):
"""
Validates if a given function can be called on each element in a column without raising an exception
"""
def __init__(self, func: typing.Callable, **kwargs):
"""
:param func: A python function that will be called with the value of each cell in the DataFrame. If this
function throws an error, this cell is considered to have failed the validation. Otherwise it has passed.
"""
if callable(type):
self.callable = func
else:
raise PanSchArgumentError('The object "{}" passed to CanCallValidation is not callable!'.format(type))
super().__init__(**kwargs)
@property
def default_message(self):
return 'raised an exception when the callable {} was called on it'.format(self.callable)
def can_call(self, var):
try:
self.callable(var)
return True
except:
return False
def validate(self, series: pd.Series) -> pd.Series:
return series.apply(self.can_call)
class CanConvertValidation(CanCallValidation):
"""
Checks if each element in a column can be converted to a Python object type
"""
"""
Internally this uses the same logic as CanCallValidation since all types are callable in python.
However this class overrides the error messages to make them more directed towards types
"""
def __init__(self, _type: type, **kwargs):
"""
:param _type: Any python type. Its constructor will be called with the value of the individual cell as its
only argument. If it throws an exception, the value is considered to fail the validation, otherwise it has passed
"""
if isinstance(_type, type):
super(CanConvertValidation, self).__init__(_type, **kwargs)
else:
raise PanSchArgumentError('{} is not a valid type'.format(_type))
@property
def default_message(self):
return 'cannot be converted to type {}'.format(self.callable)
class MatchesPatternValidation(_SeriesValidation):
"""
Validates that a string or regular expression can match somewhere in each element in this column
"""
def __init__(self, pattern, options={}, **kwargs):
"""
:param kwargs: Arguments to pass to Series.str.contains
(http://pandas.pydata.org/pandas-docs/stable/generated/pandas.Series.str.contains.html)
pat is the only required argument
"""
self.pattern = pattern
self.options = options
super().__init__(**kwargs)
@property
def default_message(self):
return 'does not match the pattern "{}"'.format(self.pattern)
def validate(self, series: pd.Series) -> pd.Series:
return series.astype(str).str.contains(self.pattern, **self.options)
class TrailingWhitespaceValidation(_SeriesValidation):
"""
Checks that there is no trailing whitespace in this column
"""
def __init__(self, **kwargs):
super().__init__(**kwargs)
@property
def default_message(self):
return 'contains trailing whitespace'
def validate(self, series: pd.Series) -> pd.Series:
return ~series.astype(str).str.contains('\s+$')
class LeadingWhitespaceValidation(_SeriesValidation):
"""
Checks that there is no leading whitespace in this column
"""
def __init__(self, **kwargs):
super().__init__(**kwargs)
@property
def default_message(self):
return 'contains leading whitespace'
def validate(self, series: pd.Series) -> pd.Series:
return ~series.astype(str).str.contains('^\s+')
class IsDistinctValidation(_SeriesValidation):
"""
Checks that every element of this column is different from each other element
"""
def __init__(self, **kwargs):
super().__init__(**kwargs)
@property
def default_message(self):
return 'contains values that are not unique'
def validate(self, series: pd.Series) -> pd.Series:
return ~series.duplicated(keep='first')
class InListValidation(_SeriesValidation):
"""
Checks that each element in this column is contained within a list of possibilities
"""
def __init__(self, options: typing.Iterable, case_sensitive: bool = True, **kwargs):
"""
:param options: A list of values to check. If the value of a cell is in this list, it is considered to pass the
validation
"""
self.case_sensitive = case_sensitive
self.options = options
super().__init__(**kwargs)
@property
def default_message(self):
values = ', '.join(str(v) for v in self.options)
return 'is not in the list of legal options ({})'.format(values)
def validate(self, series: pd.Series) -> pd.Series:
if self.case_sensitive:
return series.isin(self.options)
else:
return series.str.lower().isin([s.lower() for s in self.options])
class DateFormatValidation(_SeriesValidation):
"""
Checks that each element in this column is a valid date according to a provided format string
"""
def __init__(self, date_format: str, **kwargs):
"""
:param date_format: The date format string to validate the column against. Refer to the date format code
documentation at https://docs.python.org/3/library/datetime.html#strftime-and-strptime-behavior for a full
list of format codes
"""
self.date_format = date_format
super().__init__(**kwargs)
@property
def default_message(self):
return 'does not match the date format string "{}"'.format(self.date_format)
def valid_date(self, val):
try:
datetime.datetime.strptime(val, self.date_format)
return True
except:
return False
def validate(self, series: pd.Series) -> pd.Series:
return series.astype(str).apply(self.valid_date)