-
Notifications
You must be signed in to change notification settings - Fork 141
/
test_aggregators.py
189 lines (167 loc) · 5.63 KB
/
test_aggregators.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
import pandas as pd
from pandas import Timestamp
import adtk.aggregator as aggt
def test_or_dict_of_lists():
"""
Test OrAggregator with input as a dict of lists of time stamps or time
stamp 2-tuples
"""
lists = {
"A": [
(Timestamp("2017-1-1"), Timestamp("2017-1-2")),
(Timestamp("2017-1-5"), Timestamp("2017-1-8")),
Timestamp("2017-1-10"),
],
"B": [
Timestamp("2017-1-2"),
(Timestamp("2017-1-3"), Timestamp("2017-1-6")),
Timestamp("2017-1-8"),
(Timestamp("2017-1-7"), Timestamp("2017-1-9")),
(Timestamp("2017-1-11"), Timestamp("2017-1-11")),
],
}
assert aggt.OrAggregator().aggregate(lists) == [
(Timestamp("2017-01-01 00:00:00"), Timestamp("2017-01-02 00:00:00")),
(Timestamp("2017-01-03 00:00:00"), Timestamp("2017-01-09 00:00:00")),
Timestamp("2017-1-10"),
Timestamp("2017-1-11"),
]
lists = {
"A": [
(Timestamp("2017-1-1"), Timestamp("2017-1-2")),
(Timestamp("2017-1-5"), Timestamp("2017-1-8")),
Timestamp("2017-1-10"),
],
"B": [],
}
assert aggt.OrAggregator().aggregate(lists) == [
(Timestamp("2017-1-1"), Timestamp("2017-1-2")),
(Timestamp("2017-1-5"), Timestamp("2017-1-8")),
Timestamp("2017-1-10"),
]
def test_or_df():
"""
Test OrAggregator with input as a DataFrame
"""
df = pd.DataFrame(
[[1, 1], [1, 0], [0, 1], [0, 0], [float("nan"), 1], [0, float("nan")]],
index=pd.date_range(start="2017-1-1", periods=6, freq="D"),
)
pd.testing.assert_series_equal(
aggt.OrAggregator().aggregate(df),
pd.Series(
[1, 1, 1, 0, 1, float("nan")],
index=pd.date_range(start="2017-1-1", periods=6, freq="D"),
),
)
def test_or_dict_of_dfs():
"""
Test OrAggregator with input as a dict of DataFrame
"""
df1 = pd.DataFrame(
[[1, 1], [1, 0], [0, 1], [0, 0], [float("nan"), 1], [0, float("nan")]],
index=pd.date_range(start="2017-1-1", periods=6, freq="D"),
)
df2 = pd.DataFrame(
[[1, 1], [1, 0], [0, 1], [0, 0], [float("nan"), 1], [0, float("nan")]],
index=pd.date_range(start="2017-1-1", periods=6, freq="D"),
)
pd.testing.assert_series_equal(
aggt.OrAggregator().aggregate({"A": df1, "B": df2}),
pd.Series(
[1, 1, 1, 0, 1, float("nan")],
index=pd.date_range(start="2017-1-1", periods=6, freq="D"),
),
)
def test_and_dict_of_lists():
"""
Test AndAggregator with input as a dict of lists of time stamps or time
stamp 2-tuples
"""
lists = {
"A": [
(Timestamp("2017-1-1"), Timestamp("2017-1-2")),
(Timestamp("2017-1-5"), Timestamp("2017-1-8")),
Timestamp("2017-1-10"),
],
"B": [
Timestamp("2017-1-2"),
(Timestamp("2017-1-3"), Timestamp("2017-1-6")),
Timestamp("2017-1-8"),
(Timestamp("2017-1-7"), Timestamp("2017-1-9")),
(Timestamp("2017-1-11"), Timestamp("2017-1-11")),
],
}
assert aggt.AndAggregator().aggregate(lists) == [
Timestamp("2017-1-2"),
(Timestamp("2017-01-05 00:00:00"), Timestamp("2017-01-06 00:00:00")),
(Timestamp("2017-1-7 00:00:00"), Timestamp("2017-1-8 00:00:00")),
]
lists = {
"A": [
(Timestamp("2017-1-1"), Timestamp("2017-1-2")),
(Timestamp("2017-1-5"), Timestamp("2017-1-8")),
Timestamp("2017-1-10"),
],
"B": [],
}
assert aggt.AndAggregator().aggregate(lists) == []
def test_and_df():
"""
Test AndAggregator with input as a DataFrame
"""
df = pd.DataFrame(
[[1, 1], [1, 0], [0, 1], [0, 0], [float("nan"), 1], [0, float("nan")]],
index=pd.date_range(start="2017-1-1", periods=6, freq="D"),
)
pd.testing.assert_series_equal(
aggt.AndAggregator().aggregate(df),
pd.Series(
[1, 0, 0, 0, float("nan"), 0],
index=pd.date_range(start="2017-1-1", periods=6, freq="D"),
),
)
def test_and_dict_of_dfs():
"""
Test AndAggregator with input as a dict of DataFrame
"""
df1 = pd.DataFrame(
[[1, 1], [1, 0], [0, 1], [0, 0], [float("nan"), 1], [0, float("nan")]],
index=pd.date_range(start="2017-1-1", periods=6, freq="D"),
)
df2 = pd.DataFrame(
[[1, 1], [1, 0], [0, 1], [0, 0], [float("nan"), 1], [0, float("nan")]],
index=pd.date_range(start="2017-1-1", periods=6, freq="D"),
)
pd.testing.assert_series_equal(
aggt.AndAggregator().aggregate({"A": df1, "B": df2}),
pd.Series(
[1, 0, 0, 0, float("nan"), 0],
index=pd.date_range(start="2017-1-1", periods=6, freq="D"),
),
)
def test_customized_aggregator():
"""
Test customized aggregate
"""
def myAggFunc(df, agg="and"):
if agg == "and":
return df.all(axis=1)
elif agg == "or":
return df.any(axis=1)
else:
raise ValueError("`agg` must be either 'and' or 'or'.")
model = aggt.CustomizedAggregator(myAggFunc)
df = pd.DataFrame(
[[1, 1], [1, 0], [0, 1], [0, 0]],
index=pd.date_range(start="2017-1-1", periods=4, freq="D"),
)
pd.testing.assert_series_equal(
model.aggregate(df),
pd.Series([True, False, False, False], index=df.index),
)
model.aggregate_func_params = {"agg": "or"}
pd.testing.assert_series_equal(
model.aggregate(df),
pd.Series([True, True, True, False], index=df.index),
)