forked from engarde-dev/engarde
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generic.py
80 lines (68 loc) · 1.79 KB
/
generic.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
# -*- coding: utf-8 -*-
"""
Module for useful generic functions.
"""
from itertools import chain, cycle
import numpy as np
import pandas as pd
# --------------
# Generic verify
# --------------
def verify(df, check, *args, **kwargs):
"""
Generic verify. Assert that ``check(df, *args, **kwargs)`` is
true.
Parameters
==========
df : DataFrame
check : function
Should take DataFrame and **kwargs. Returns bool
Returns
=======
df : DataFrame
same as the input.
"""
result = check(df, *args, **kwargs)
try:
assert result
except AssertionError as e:
msg = '{} is not true'.format(check.__name__)
e.args = (msg, df)
raise
return df
def verify_all(df, check, *args, **kwargs):
"""
Verify that all the entries in ``check(df, *args, **kwargs)``
are true.
"""
result = check(df, *args, **kwargs)
try:
assert np.all(result)
except AssertionError as e:
msg = "{} not true for all".format(check.__name__)
e.args = (msg, df[~result])
raise
return df
def verify_any(df, check, *args, **kwargs):
"""
Verify that any of the entries in ``check(df, *args, **kwargs)``
is true
"""
result = check(df, *args, **kwargs)
try:
assert np.any(result)
except AssertionError as e:
msg = '{} not true for any'.format(check.__name__)
e.args = (msg, df)
raise
return df
# ---------------
# Error reporting
# ---------------
def bad_locations(df):
columns = df.columns
all_locs = chain.from_iterable(zip(df.index, cycle([col])) for col in columns)
bad = pd.Series(list(all_locs))[np.asarray(df).ravel(1)]
msg = bad.values
return msg
__all__ = ['verify', 'verify_all', 'verify_any', 'bad_locations']