Debian Codegraph @

petl/test/transform/test_reshape.py

raw
1 from __future__ import absolute_import, print_function, division
2
3
4 from datetime import datetime
5
6 import pytest
7
8 from petl.errors import FieldSelectionError
9 from petl.test.helpers import ieq
10 from petl.transform.reshape import melt, recast, transpose, pivot, flatten, \
11 unflatten
12 from petl.transform.regex import split, capture
13
14
15 def test_melt_1():
16
17 table = (('id', 'gender', 'age'),
18 (1, 'F', 12),
19 (2, 'M', 17),
20 (3, 'M', 16))
21
22 expectation = (('id', 'variable', 'value'),
23 (1, 'gender', 'F'),
24 (1, 'age', 12),
25 (2, 'gender', 'M'),
26 (2, 'age', 17),
27 (3, 'gender', 'M'),
28 (3, 'age', 16))
29
30 result = melt(table, key='id')
31 ieq(expectation, result)
32
33 # use field index as key
34 result = melt(table, key=0)
35 ieq(expectation, result)
36
37 result = melt(table, key='id', variablefield='variable', valuefield='value')
38 ieq(expectation, result)
39
40
41 def test_melt_2():
42
43 table = (('id', 'time', 'height', 'weight'),
44 (1, 11, 66.4, 12.2),
45 (2, 16, 53.2, 17.3),
46 (3, 12, 34.5, 9.4))
47
48 expectation = (('id', 'time', 'variable', 'value'),
49 (1, 11, 'height', 66.4),
50 (1, 11, 'weight', 12.2),
51 (2, 16, 'height', 53.2),
52 (2, 16, 'weight', 17.3),
53 (3, 12, 'height', 34.5),
54 (3, 12, 'weight', 9.4))
55 result = melt(table, key=('id', 'time'))
56 ieq(expectation, result)
57
58 expectation = (('id', 'time', 'variable', 'value'),
59 (1, 11, 'height', 66.4),
60 (2, 16, 'height', 53.2),
61 (3, 12, 'height', 34.5))
62 result = melt(table, key=('id', 'time'), variables='height')
63 print(result)
64 ieq(expectation, result)
65
66
67 def test_melt_empty():
68 table = (('foo', 'bar', 'baz'),)
69 expect = (('foo', 'variable', 'value'),)
70 actual = melt(table, key='foo')
71 ieq(expect, actual)
72
73
74 def test_melt_headerless():
75 table = []
76 expect = []
77 actual = melt(table, key='foo')
78 ieq(expect, actual)
79
80
81 def test_melt_1_shortrow():
82
83 table = (('id', 'gender', 'age'),
84 (1, 'F', 12),
85 (2, 'M', 17),
86 (3, 'M'),
87 (4,))
88
89 expectation = (('id', 'variable', 'value'),
90 (1, 'gender', 'F'),
91 (1, 'age', 12),
92 (2, 'gender', 'M'),
93 (2, 'age', 17),
94 (3, 'gender', 'M'))
95
96 result = melt(table, key='id')
97 ieq(expectation, result)
98
99 result = melt(table, key='id', variablefield='variable', valuefield='value')
100 ieq(expectation, result)
101
102
103 def test_melt_2_shortrow():
104
105 table = (('id', 'time', 'height', 'weight'),
106 (1, 11, 66.4, 12.2),
107 (2, 16, 53.2, 17.3),
108 (3, 12, 34.5),
109 (4, 14))
110
111 expectation = (('id', 'time', 'variable', 'value'),
112 (1, 11, 'height', 66.4),
113 (1, 11, 'weight', 12.2),
114 (2, 16, 'height', 53.2),
115 (2, 16, 'weight', 17.3),
116 (3, 12, 'height', 34.5))
117 result = melt(table, key=('id', 'time'))
118 ieq(expectation, result)
119
120 expectation = (('id', 'time', 'variable', 'value'),
121 (1, 11, 'height', 66.4),
122 (2, 16, 'height', 53.2),
123 (3, 12, 'height', 34.5))
124 result = melt(table, key=('id', 'time'), variables='height')
125 ieq(expectation, result)
126
127
128 def test_recast_1():
129
130 table = (('id', 'variable', 'value'),
131 (3, 'age', 16),
132 (1, 'gender', 'F'),
133 (2, 'gender', 'M'),
134 (2, 'age', 17),
135 (1, 'age', 12),
136 (3, 'gender', 'M'))
137
138 expectation = (('id', 'age', 'gender'),
139 (1, 12, 'F'),
140 (2, 17, 'M'),
141 (3, 16, 'M'))
142
143 # by default lift 'variable' field, hold everything else
144 result = recast(table)
145 ieq(expectation, result)
146
147 result = recast(table, variablefield='variable')
148 ieq(expectation, result)
149
150 result = recast(table, key='id', variablefield='variable')
151 ieq(expectation, result)
152
153 result = recast(table, key='id', variablefield='variable',
154 valuefield='value')
155 ieq(expectation, result)
156
157
158 def test_recast_2():
159
160 table = (('id', 'variable', 'value'),
161 (3, 'age', 16),
162 (1, 'gender', 'F'),
163 (2, 'gender', 'M'),
164 (2, 'age', 17),
165 (1, 'age', 12),
166 (3, 'gender', 'M'))
167
168 expectation = (('id', 'gender'),
169 (1, 'F'),
170 (2, 'M'),
171 (3, 'M'))
172
173 # can manually pick which variables you want to recast as fields
174 # TODO this is awkward
175 result = recast(table, key='id', variablefield={'variable': ['gender']})
176 ieq(expectation, result)
177
178
179 def test_recast_3():
180
181 table = (('id', 'time', 'variable', 'value'),
182 (1, 11, 'weight', 66.4),
183 (1, 14, 'weight', 55.2),
184 (2, 12, 'weight', 53.2),
185 (2, 16, 'weight', 43.3),
186 (3, 12, 'weight', 34.5),
187 (3, 17, 'weight', 49.4))
188
189 expectation = (('id', 'time', 'weight'),
190 (1, 11, 66.4),
191 (1, 14, 55.2),
192 (2, 12, 53.2),
193 (2, 16, 43.3),
194 (3, 12, 34.5),
195 (3, 17, 49.4))
196 result = recast(table)
197 ieq(expectation, result)
198
199 # in the absence of an aggregation function, list all values
200 expectation = (('id', 'weight'),
201 (1, [66.4, 55.2]),
202 (2, [53.2, 43.3]),
203 (3, [34.5, 49.4]))
204 result = recast(table, key='id')
205 ieq(expectation, result)
206
207 # max aggregation
208 expectation = (('id', 'weight'),
209 (1, 66.4),
210 (2, 53.2),
211 (3, 49.4))
212 result = recast(table, key='id', reducers={'weight': max})
213 ieq(expectation, result)
214
215 # min aggregation
216 expectation = (('id', 'weight'),
217 (1, 55.2),
218 (2, 43.3),
219 (3, 34.5))
220 result = recast(table, key='id', reducers={'weight': min})
221 ieq(expectation, result)
222
223 # mean aggregation
224 expectation = (('id', 'weight'),
225 (1, 60.80),
226 (2, 48.25),
227 (3, 41.95))
228
229 def mean(values):
230 return float(sum(values)) / len(values)
231
232 def meanf(precision):
233 def f(values):
234 v = mean(values)
235 v = round(v, precision)
236 return v
237 return f
238
239 result = recast(table, key='id', reducers={'weight': meanf(precision=2)})
240 ieq(expectation, result)
241
242
243 def test_recast4():
244
245 # deal with missing data
246 table = (('id', 'variable', 'value'),
247 (1, 'gender', 'F'),
248 (2, 'age', 17),
249 (1, 'age', 12),
250 (3, 'gender', 'M'))
251 result = recast(table, key='id')
252 expect = (('id', 'age', 'gender'),
253 (1, 12, 'F'),
254 (2, 17, None),
255 (3, None, 'M'))
256 ieq(expect, result)
257
258
259 def test_recast_empty():
260 table = (('foo', 'variable', 'value'),)
261 expect = (('foo',),)
262 actual = recast(table)
263 ieq(expect, actual)
264
265
266 def test_recast_headerless():
267 table = []
268 expect = []
269 actual = recast(table)
270 ieq(expect, actual)
271
272
273 def test_recast_date():
274
275 dt = datetime.now().replace
276 table = (('id', 'variable', 'value'),
277 (dt(hour=3), 'age', 16),
278 (dt(hour=1), 'gender', 'F'),
279 (dt(hour=2), 'gender', 'M'),
280 (dt(hour=2), 'age', 17),
281 (dt(hour=1), 'age', 12),
282 (dt(hour=3), 'gender', 'M'))
283
284 expectation = (('id', 'age', 'gender'),
285 (dt(hour=1), 12, 'F'),
286 (dt(hour=2), 17, 'M'),
287 (dt(hour=3), 16, 'M'))
288
289 # by default lift 'variable' field, hold everything else
290 result = recast(table)
291 ieq(expectation, result)
292
293 result = recast(table, variablefield='variable')
294 ieq(expectation, result)
295
296 result = recast(table, key='id', variablefield='variable')
297 ieq(expectation, result)
298
299 result = recast(table, key='id', variablefield='variable',
300 valuefield='value')
301 ieq(expectation, result)
302
303
304 def test_melt_and_capture():
305
306 table = (('id', 'parad0', 'parad1', 'parad2'),
307 ('1', '12', '34', '56'),
308 ('2', '23', '45', '67'))
309
310 expectation = (('id', 'parasitaemia', 'day'),
311 ('1', '12', '0'),
312 ('1', '34', '1'),
313 ('1', '56', '2'),
314 ('2', '23', '0'),
315 ('2', '45', '1'),
316 ('2', '67', '2'))
317
318 step1 = melt(table, key='id', valuefield='parasitaemia')
319 step2 = capture(step1, 'variable', 'parad(\\d+)', ('day',))
320 ieq(expectation, step2)
321
322
323 def test_melt_and_split():
324
325 table = (('id', 'parad0', 'parad1', 'parad2', 'tempd0', 'tempd1', 'tempd2'),
326 ('1', '12', '34', '56', '37.2', '37.4', '37.9'),
327 ('2', '23', '45', '67', '37.1', '37.8', '36.9'))
328
329 expectation = (('id', 'value', 'variable', 'day'),
330 ('1', '12', 'para', '0'),
331 ('1', '34', 'para', '1'),
332 ('1', '56', 'para', '2'),
333 ('1', '37.2', 'temp', '0'),
334 ('1', '37.4', 'temp', '1'),
335 ('1', '37.9', 'temp', '2'),
336 ('2', '23', 'para', '0'),
337 ('2', '45', 'para', '1'),
338 ('2', '67', 'para', '2'),
339 ('2', '37.1', 'temp', '0'),
340 ('2', '37.8', 'temp', '1'),
341 ('2', '36.9', 'temp', '2'))
342
343 step1 = melt(table, key='id')
344 step2 = split(step1, 'variable', 'd', ('variable', 'day'))
345 ieq(expectation, step2)
346
347
348 def test_transpose():
349 table1 = (('id', 'colour'),
350 (1, 'blue'),
351 (2, 'red'),
352 (3, 'purple'),
353 (5, 'yellow'),
354 (7, 'orange'))
355 table2 = transpose(table1)
356 expect2 = (('id', 1, 2, 3, 5, 7),
357 ('colour', 'blue', 'red', 'purple', 'yellow', 'orange'))
358 ieq(expect2, table2)
359 ieq(expect2, table2)
360
361
362 def test_transpose_empty():
363 table1 = (('id', 'colour'),)
364 table2 = transpose(table1)
365 expect2 = (('id',),
366 ('colour',))
367 ieq(expect2, table2)
368
369
370 def test_pivot():
371
372 table1 = (('region', 'gender', 'style', 'units'),
373 ('east', 'boy', 'tee', 12),
374 ('east', 'boy', 'golf', 14),
375 ('east', 'boy', 'fancy', 7),
376 ('east', 'girl', 'tee', 3),
377 ('east', 'girl', 'golf', 8),
378 ('east', 'girl', 'fancy', 18),
379 ('west', 'boy', 'tee', 12),
380 ('west', 'boy', 'golf', 15),
381 ('west', 'boy', 'fancy', 8),
382 ('west', 'girl', 'tee', 6),
383 ('west', 'girl', 'golf', 16),
384 ('west', 'girl', 'fancy', 1))
385
386 table2 = pivot(table1, 'region', 'gender', 'units', sum)
387 expect2 = (('region', 'boy', 'girl'),
388 ('east', 33, 29),
389 ('west', 35, 23))
390 ieq(expect2, table2)
391 ieq(expect2, table2)
392
393
394 def test_pivot_empty():
395
396 table1 = (('region', 'gender', 'style', 'units'),)
397 table2 = pivot(table1, 'region', 'gender', 'units', sum)
398 expect2 = (('region',),)
399 ieq(expect2, table2)
400
401
402 def test_pivot_headerless():
403 table1 = []
404 with pytest.raises(FieldSelectionError):
405 for i in pivot(table1, 'region', 'gender', 'units', sum):
406 pass
407
408
409 def test_flatten():
410
411 table1 = (('foo', 'bar', 'baz'),
412 ('A', 1, True),
413 ('C', 7, False),
414 ('B', 2, False),
415 ('C', 9, True))
416
417 expect1 = ('A', 1, True, 'C', 7, False, 'B', 2, False, 'C', 9, True)
418
419 actual1 = flatten(table1)
420 ieq(expect1, actual1)
421 ieq(expect1, actual1)
422
423
424 def test_flatten_empty():
425
426 table1 = (('foo', 'bar', 'baz'),)
427 expect1 = []
428 actual1 = flatten(table1)
429 ieq(expect1, actual1)
430
431
432 def test_flatten_headerless():
433 table1 = []
434 expect1 = []
435 actual1 = flatten(table1)
436 ieq(expect1, actual1)
437
438
439 def test_unflatten():
440
441 table1 = (('lines',),
442 ('A',),
443 (1,),
444 (True,),
445 ('C',),
446 (7,),
447 (False,),
448 ('B',),
449 (2,),
450 (False,),
451 ('C',),
452 (9,))
453
454 expect1 = (('f0', 'f1', 'f2'),
455 ('A', 1, True),
456 ('C', 7, False),
457 ('B', 2, False),
458 ('C', 9, None))
459
460 actual1 = unflatten(table1, 'lines', 3)
461
462 ieq(expect1, actual1)
463 ieq(expect1, actual1)
464
465
466 def test_unflatten_2():
467
468 inpt = ('A', 1, True, 'C', 7, False, 'B', 2, False, 'C', 9)
469
470 expect1 = (('f0', 'f1', 'f2'),
471 ('A', 1, True),
472 ('C', 7, False),
473 ('B', 2, False),
474 ('C', 9, None))
475
476 actual1 = unflatten(inpt, 3)
477
478 ieq(expect1, actual1)
479 ieq(expect1, actual1)
480
481
482 def test_unflatten_empty():
483
484 table1 = (('lines',),)
485 expect1 = (('f0', 'f1', 'f2'),)
486 actual1 = unflatten(table1, 'lines', 3)
487 ieq(expect1, actual1)