-
Notifications
You must be signed in to change notification settings - Fork 20
/
test_pandas.py
292 lines (223 loc) · 11.2 KB
/
test_pandas.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
import copy
from unittest import TestCase
import numpy as np
import pandas as pd
import pandas.testing
from fireant.slicer.widgets.pandas import Pandas
from fireant.tests.slicer.mocks import (
CumSum,
ElectionOverElection,
cat_dim_df,
cont_cat_dim_df,
cont_dim_df,
cont_dim_operation_df,
cont_uni_dim_df,
cont_uni_dim_ref_df,
multi_metric_df,
single_metric_df,
slicer,
uni_dim_df,
)
from fireant.utils import (
format_dimension_key as fd,
format_metric_key as fm,
)
class PandasTransformerTests(TestCase):
maxDiff = None
def test_single_metric(self):
result = Pandas(slicer.metrics.votes) \
.transform(single_metric_df, slicer, [], [])
expected = single_metric_df.copy()[[fm('votes')]]
expected.columns = ['Votes']
expected.columns.name = 'Metrics'
pandas.testing.assert_frame_equal(expected, result)
def test_multiple_metrics(self):
result = Pandas(slicer.metrics.votes, slicer.metrics.wins) \
.transform(multi_metric_df, slicer, [], [])
expected = multi_metric_df.copy()[[fm('votes'), fm('wins')]]
expected.columns = ['Votes', 'Wins']
expected.columns.name = 'Metrics'
pandas.testing.assert_frame_equal(expected, result)
def test_multiple_metrics_reversed(self):
result = Pandas(slicer.metrics.wins, slicer.metrics.votes) \
.transform(multi_metric_df, slicer, [], [])
expected = multi_metric_df.copy()[[fm('wins'), fm('votes')]]
expected.columns = ['Wins', 'Votes']
expected.columns.name = 'Metrics'
pandas.testing.assert_frame_equal(expected, result)
def test_time_series_dim(self):
result = Pandas(slicer.metrics.wins) \
.transform(cont_dim_df, slicer, [slicer.dimensions.timestamp], [])
expected = cont_dim_df.copy()[[fm('wins')]]
expected.index.names = ['Timestamp']
expected.columns = ['Wins']
expected.columns.name = 'Metrics'
pandas.testing.assert_frame_equal(expected, result)
def test_time_series_dim_with_operation(self):
result = Pandas(CumSum(slicer.metrics.votes)) \
.transform(cont_dim_operation_df, slicer, [slicer.dimensions.timestamp], [])
expected = cont_dim_operation_df.copy()[[fm('cumsum(votes)')]]
expected.index.names = ['Timestamp']
expected.columns = ['CumSum(Votes)']
expected.columns.name = 'Metrics'
pandas.testing.assert_frame_equal(expected, result)
def test_cat_dim(self):
result = Pandas(slicer.metrics.wins) \
.transform(cat_dim_df, slicer, [slicer.dimensions.political_party], [])
expected = cat_dim_df.copy()[[fm('wins')]]
expected.index = pd.Index(['Democrat', 'Independent', 'Republican'], name='Party')
expected.columns = ['Wins']
expected.columns.name = 'Metrics'
pandas.testing.assert_frame_equal(expected, result)
def test_uni_dim(self):
result = Pandas(slicer.metrics.wins) \
.transform(uni_dim_df, slicer, [slicer.dimensions.candidate], [])
expected = uni_dim_df.copy() \
.set_index(fd('candidate_display'), append=True) \
.reset_index(fd('candidate'), drop=True) \
[[fm('wins')]]
expected.index.names = ['Candidate']
expected.columns = ['Wins']
expected.columns.name = 'Metrics'
pandas.testing.assert_frame_equal(expected, result)
def test_uni_dim_no_display_definition(self):
import copy
candidate = copy.copy(slicer.dimensions.candidate)
candidate.display_key = None
candidate.display_definition = None
uni_dim_df_copy = uni_dim_df.copy()
del uni_dim_df_copy[fd(slicer.dimensions.candidate.display_key)]
result = Pandas(slicer.metrics.wins) \
.transform(uni_dim_df_copy, slicer, [candidate], [])
expected = uni_dim_df_copy.copy()[[fm('wins')]]
expected.index.names = ['Candidate']
expected.columns = ['Wins']
expected.columns.name = 'Metrics'
pandas.testing.assert_frame_equal(expected, result)
def test_multi_dims_time_series_and_uni(self):
result = Pandas(slicer.metrics.wins) \
.transform(cont_uni_dim_df, slicer, [slicer.dimensions.timestamp, slicer.dimensions.state], [])
expected = cont_uni_dim_df.copy() \
.set_index(fd('state_display'), append=True) \
.reset_index(fd('state'), drop=False)[[fm('wins')]]
expected.index.names = ['Timestamp', 'State']
expected.columns = ['Wins']
expected.columns.name = 'Metrics'
pandas.testing.assert_frame_equal(expected, result)
def test_transpose_single_dimension(self):
result = Pandas(slicer.metrics.wins, transpose=True) \
.transform(cat_dim_df, slicer, [slicer.dimensions.political_party], [])
expected = cat_dim_df.copy()[[fm('wins')]]
expected.index = pd.Index(['Democrat', 'Independent', 'Republican'], name='Party')
expected.columns = ['Wins']
expected.columns.name = 'Metrics'
expected = expected.transpose()
pandas.testing.assert_frame_equal(expected, result)
def test_pivoted_single_dimension_transposes_data_frame(self):
result = Pandas(slicer.metrics.wins, pivot=[slicer.dimensions.political_party]) \
.transform(cat_dim_df, slicer, [slicer.dimensions.political_party], [])
expected = cat_dim_df.copy()[[fm('wins')]]
expected.index = pd.Index(['Democrat', 'Independent', 'Republican'], name='Party')
expected.columns = ['Wins']
expected.columns.name = 'Metrics'
expected = expected.transpose()
pandas.testing.assert_frame_equal(expected, result)
def test_pivoted_multi_dims_time_series_and_cat(self):
result = Pandas(slicer.metrics.wins, pivot=[slicer.dimensions.political_party]) \
.transform(cont_cat_dim_df, slicer, [slicer.dimensions.timestamp, slicer.dimensions.political_party], [])
expected = cont_cat_dim_df.copy()[[fm('wins')]]
expected = expected.unstack(level=[1]).fillna(value='')
expected.index.names = ['Timestamp']
expected.columns = ['Democrat', 'Independent', 'Republican']
expected.columns.names = ['Party']
pandas.testing.assert_frame_equal(expected, result)
def test_pivoted_multi_dims_time_series_and_uni(self):
result = Pandas(slicer.metrics.votes, pivot=[slicer.dimensions.state]) \
.transform(cont_uni_dim_df, slicer, [slicer.dimensions.timestamp, slicer.dimensions.state], [])
expected = cont_uni_dim_df.copy() \
.set_index(fd('state_display'), append=True) \
.reset_index(fd('state'), drop=True)[[fm('votes')]]
expected = expected.unstack(level=[1])
expected.index.names = ['Timestamp']
expected.columns = ['California', 'Texas']
expected.columns.names = ['State']
pandas.testing.assert_frame_equal(expected, result)
def test_time_series_ref(self):
result = Pandas(slicer.metrics.votes) \
.transform(cont_uni_dim_ref_df, slicer,
[
slicer.dimensions.timestamp,
slicer.dimensions.state
], [
ElectionOverElection(slicer.dimensions.timestamp)
])
expected = cont_uni_dim_ref_df.copy() \
.set_index(fd('state_display'), append=True) \
.reset_index(fd('state'), drop=True)[[fm('votes'), fm('votes_eoe')]]
expected.index.names = ['Timestamp', 'State']
expected.columns = ['Votes', 'Votes (EoE)']
expected.columns.name = 'Metrics'
pandas.testing.assert_frame_equal(expected, result)
def test_metric_format(self):
import copy
votes = copy.copy(slicer.metrics.votes)
votes.prefix = '$'
votes.suffix = '€'
votes.precision = 2
# divide the data frame by 3 to get a repeating decimal so we can check precision
result = Pandas(votes) \
.transform(cont_dim_df / 3, slicer, [slicer.dimensions.timestamp], [])
expected = cont_dim_df.copy()[[fm('votes')]]
expected[fm('votes')] = ['${0:,.2f}€'.format(x)
for x in expected[fm('votes')] / 3]
expected.index.names = ['Timestamp']
expected.columns = ['Votes']
expected.columns.name = 'Metrics'
pandas.testing.assert_frame_equal(expected, result)
def test_nan_in_metrics(self):
cat_dim_df_with_nan = cat_dim_df.copy()
cat_dim_df_with_nan['$m$wins'] = cat_dim_df_with_nan['$m$wins'].apply(float)
cat_dim_df_with_nan.iloc[2, 1] = np.nan
result = Pandas(slicer.metrics.wins) \
.transform(cat_dim_df_with_nan, slicer, [slicer.dimensions.political_party], [])
expected = cat_dim_df_with_nan.copy()[[fm('wins')]]
expected.index = pd.Index(['Democrat', 'Independent', 'Republican'], name='Party')
expected.columns = ['Wins']
expected.columns.name = 'Metrics'
pandas.testing.assert_frame_equal(expected, result)
def test_inf_in_metrics(self):
cat_dim_df_with_nan = cat_dim_df.copy()
cat_dim_df_with_nan['$m$wins'] = cat_dim_df_with_nan['$m$wins'].apply(float)
cat_dim_df_with_nan.iloc[2, 1] = np.inf
result = Pandas(slicer.metrics.wins) \
.transform(cat_dim_df_with_nan, slicer, [slicer.dimensions.political_party], [])
expected = cat_dim_df_with_nan.copy()[[fm('wins')]]
expected.index = pd.Index(['Democrat', 'Independent', 'Republican'], name='Party')
expected.columns = ['Wins']
expected.columns.name = 'Metrics'
pandas.testing.assert_frame_equal(expected, result)
def test_neginf_in_metrics(self):
cat_dim_df_with_nan = cat_dim_df.copy()
cat_dim_df_with_nan['$m$wins'] = cat_dim_df_with_nan['$m$wins'].apply(float)
cat_dim_df_with_nan.iloc[2, 1] = np.inf
result = Pandas(slicer.metrics.wins) \
.transform(cat_dim_df_with_nan, slicer, [slicer.dimensions.political_party], [])
expected = cat_dim_df_with_nan.copy()[[fm('wins')]]
expected.index = pd.Index(['Democrat', 'Independent', 'Republican'], name='Party')
expected.columns = ['Wins']
expected.columns.name = 'Metrics'
pandas.testing.assert_frame_equal(expected, result)
def test_inf_in_metrics_with_precision_zero(self):
cat_dim_df_with_nan = cat_dim_df.copy()
cat_dim_df_with_nan['$m$wins'] = cat_dim_df_with_nan['$m$wins'].apply(float)
cat_dim_df_with_nan.iloc[2, 1] = np.inf
slicer_modified = copy.deepcopy(slicer)
slicer_modified.metrics.wins.precision = 0
result = Pandas(slicer_modified.metrics.wins) \
.transform(cat_dim_df_with_nan, slicer_modified, [slicer_modified.dimensions.political_party], [])
expected = cat_dim_df_with_nan.copy()[[fm('wins')]]
expected.index = pd.Index(['Democrat', 'Independent', 'Republican'], name='Party')
expected['$m$wins'] = ['6', '0', '']
expected.columns = ['Wins']
expected.columns.name = 'Metrics'
pandas.testing.assert_frame_equal(expected, result)