-
Notifications
You must be signed in to change notification settings - Fork 192
/
counting.py
469 lines (361 loc) · 14 KB
/
counting.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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
from __future__ import absolute_import, print_function, division
from collections import Counter
from petl.compat import string_types, maketrans
from petl.util.base import values, Table, data, wrap
def nrows(table):
"""
Count the number of data rows in a table. E.g.::
>>> import petl as etl
>>> table = [['foo', 'bar'], ['a', 1], ['b', 2]]
>>> etl.nrows(table)
2
"""
return sum(1 for _ in data(table))
Table.nrows = nrows
def valuecount(table, field, value, missing=None):
"""
Count the number of occurrences of `value` under the given field. Returns
the absolute count and relative frequency as a pair. E.g.::
>>> import petl as etl
>>> table = [['foo', 'bar'],
... ['a', 1],
... ['b', 2],
... ['b', 7]]
>>> etl.valuecount(table, 'foo', 'b')
(2, 0.6666666666666666)
The `field` argument can be a single field name or index (starting from
zero) or a tuple of field names and/or indexes.
"""
total = 0
vs = 0
for v in values(table, field, missing=missing):
total += 1
if v == value:
vs += 1
return vs, float(vs)/total
Table.valuecount = valuecount
def valuecounter(table, *field, **kwargs):
"""
Find distinct values for the given field and count the number of
occurrences. Returns a :class:`dict` mapping values to counts. E.g.::
>>> import petl as etl
>>> table = [['foo', 'bar'],
... ['a', True],
... ['b'],
... ['b', True],
... ['c', False]]
>>> etl.valuecounter(table, 'foo')
Counter({'b': 2, 'a': 1, 'c': 1})
The `field` argument can be a single field name or index (starting from
zero) or a tuple of field names and/or indexes.
"""
missing = kwargs.get('missing', None)
counter = Counter()
for v in values(table, field, missing=missing):
try:
counter[v] += 1
except IndexError:
pass # short row
return counter
Table.valuecounter = valuecounter
def valuecounts(table, *field, **kwargs):
"""
Find distinct values for the given field and count the number and relative
frequency of occurrences. Returns a table mapping values to counts, with
most common values first. E.g.::
>>> import petl as etl
>>> table = [['foo', 'bar', 'baz'],
... ['a', True, 0.12],
... ['a', True, 0.17],
... ['b', False, 0.34],
... ['b', False, 0.44],
... ['b']]
>>> etl.valuecounts(table, 'foo')
+-----+-------+-----------+
| foo | count | frequency |
+=====+=======+===========+
| 'b' | 3 | 0.6 |
+-----+-------+-----------+
| 'a' | 2 | 0.4 |
+-----+-------+-----------+
>>> etl.valuecounts(table, 'foo', 'bar')
+-----+-------+-------+-----------+
| foo | bar | count | frequency |
+=====+=======+=======+===========+
| 'a' | True | 2 | 0.4 |
+-----+-------+-------+-----------+
| 'b' | False | 2 | 0.4 |
+-----+-------+-------+-----------+
| 'b' | None | 1 | 0.2 |
+-----+-------+-------+-----------+
If rows are short, the value of the keyword argument `missing` is counted.
Multiple fields can be given as positional arguments. If multiple fields are
given, these are treated as a compound key.
"""
return ValueCountsView(table, field, **kwargs)
Table.valuecounts = valuecounts
class ValueCountsView(Table):
def __init__(self, table, field, missing=None):
self.table = table
self.field = field
self.missing = missing
def __iter__(self):
# construct output header
if isinstance(self.field, (tuple, list)):
outhdr = tuple(self.field) + ('count', 'frequency')
else:
outhdr = (self.field, 'count', 'frequency')
yield outhdr
# count values
counter = valuecounter(self.table, *self.field, missing=self.missing)
counts = counter.most_common() # sort descending
total = sum(c[1] for c in counts)
if len(self.field) > 1:
for c in counts:
yield tuple(c[0]) + (c[1], float(c[1])/total)
else:
for c in counts:
yield (c[0], c[1], float(c[1])/total)
def parsecounter(table, field, parsers=(('int', int), ('float', float))):
"""
Count the number of `str` or `unicode` values under the given fields that
can be parsed as ints, floats or via custom parser functions. Return a
pair of `Counter` objects, the first mapping parser names to the number of
strings successfully parsed, the second mapping parser names to the
number of errors. E.g.::
>>> import petl as etl
>>> table = [['foo', 'bar', 'baz'],
... ['A', 'aaa', 2],
... ['B', u'2', '3.4'],
... [u'B', u'3', u'7.8', True],
... ['D', '3.7', 9.0],
... ['E', 42]]
>>> counter, errors = etl.parsecounter(table, 'bar')
>>> counter
Counter({'float': 3, 'int': 2})
>>> errors
Counter({'int': 2, 'float': 1})
The `field` argument can be a field name or index (starting from zero).
"""
if isinstance(parsers, (list, tuple)):
parsers = dict(parsers)
counter, errors = Counter(), Counter()
# need to initialise
for n in parsers.keys():
counter[n] = 0
errors[n] = 0
for v in values(table, field):
if isinstance(v, string_types):
for name, parser in parsers.items():
try:
parser(v)
except:
errors[name] += 1
else:
counter[name] += 1
return counter, errors
Table.parsecounter = parsecounter
def parsecounts(table, field, parsers=(('int', int), ('float', float))):
"""
Count the number of `str` or `unicode` values that can be parsed as ints,
floats or via custom parser functions. Return a table mapping parser names
to the number of values successfully parsed and the number of errors. E.g.::
>>> import petl as etl
>>> table = [['foo', 'bar', 'baz'],
... ['A', 'aaa', 2],
... ['B', u'2', '3.4'],
... [u'B', u'3', u'7.8', True],
... ['D', '3.7', 9.0],
... ['E', 42]]
>>> etl.parsecounts(table, 'bar')
+---------+-------+--------+
| type | count | errors |
+=========+=======+========+
| 'float' | 3 | 1 |
+---------+-------+--------+
| 'int' | 2 | 2 |
+---------+-------+--------+
The `field` argument can be a field name or index (starting from zero).
"""
return ParseCountsView(table, field, parsers=parsers)
Table.parsecounts = parsecounts
class ParseCountsView(Table):
def __init__(self, table, field, parsers=(('int', int), ('float', float))):
self.table = table
self.field = field
if isinstance(parsers, (list, tuple)):
parsers = dict(parsers)
self.parsers = parsers
def __iter__(self):
counter, errors = parsecounter(self.table, self.field, self.parsers)
yield ('type', 'count', 'errors')
for (item, n) in counter.most_common():
yield (item, n, errors[item])
def typecounter(table, field):
"""
Count the number of values found for each Python type.
>>> import petl as etl
>>> table = [['foo', 'bar', 'baz'],
... ['A', 1, 2],
... ['B', u'2', '3.4'],
... [u'B', u'3', u'7.8', True],
... ['D', u'xyz', 9.0],
... ['E', 42]]
>>> etl.typecounter(table, 'foo')
Counter({'str': 5})
>>> etl.typecounter(table, 'bar')
Counter({'str': 3, 'int': 2})
>>> etl.typecounter(table, 'baz')
Counter({'str': 2, 'int': 1, 'float': 1, 'NoneType': 1})
The `field` argument can be a field name or index (starting from zero).
"""
counter = Counter()
for v in values(table, field):
try:
counter[v.__class__.__name__] += 1
except IndexError:
pass # ignore short rows
return counter
Table.typecounter = typecounter
def typecounts(table, field):
"""
Count the number of values found for each Python type and return a table
mapping class names to counts and frequencies. E.g.::
>>> import petl as etl
>>> table = [['foo', 'bar', 'baz'],
... [b'A', 1, 2],
... [b'B', '2', b'3.4'],
... ['B', '3', '7.8', True],
... ['D', u'xyz', 9.0],
... ['E', 42]]
>>> etl.typecounts(table, 'foo')
+---------+-------+-----------+
| type | count | frequency |
+=========+=======+===========+
| 'str' | 3 | 0.6 |
+---------+-------+-----------+
| 'bytes' | 2 | 0.4 |
+---------+-------+-----------+
>>> etl.typecounts(table, 'bar')
+-------+-------+-----------+
| type | count | frequency |
+=======+=======+===========+
| 'str' | 3 | 0.6 |
+-------+-------+-----------+
| 'int' | 2 | 0.4 |
+-------+-------+-----------+
>>> etl.typecounts(table, 'baz')
+------------+-------+-----------+
| type | count | frequency |
+============+=======+===========+
| 'int' | 1 | 0.2 |
+------------+-------+-----------+
| 'bytes' | 1 | 0.2 |
+------------+-------+-----------+
| 'str' | 1 | 0.2 |
+------------+-------+-----------+
| 'float' | 1 | 0.2 |
+------------+-------+-----------+
| 'NoneType' | 1 | 0.2 |
+------------+-------+-----------+
The `field` argument can be a field name or index (starting from zero).
"""
return TypeCountsView(table, field)
Table.typecounts = typecounts
class TypeCountsView(Table):
def __init__(self, table, field):
self.table = table
self.field = field
def __iter__(self):
counter = typecounter(self.table, self.field)
yield ('type', 'count', 'frequency')
counts = counter.most_common()
total = sum(c[1] for c in counts)
for c in counts:
yield (c[0], c[1], float(c[1])/total)
def stringpatterncounter(table, field):
"""
Profile string patterns in the given field, returning a :class:`dict`
mapping patterns to counts.
"""
trans = maketrans(
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789',
'AAAAAAAAAAAAAAAAAAAAAAAAAAaaaaaaaaaaaaaaaaaaaaaaaaaa9999999999'
)
counter = Counter()
for v in values(table, field):
p = str(v).translate(trans)
counter[p] += 1
return counter
Table.stringpatterncounter = stringpatterncounter
def stringpatterns(table, field):
"""
Profile string patterns in the given field, returning a table of patterns,
counts and frequencies. E.g.::
>>> import petl as etl
>>> table = [['foo', 'bar'],
... ['Mr. Foo', '123-1254'],
... ['Mrs. Bar', '234-1123'],
... ['Mr. Spo', '123-1254'],
... [u'Mr. Baz', u'321 1434'],
... [u'Mrs. Baz', u'321 1434'],
... ['Mr. Quux', '123-1254-XX']]
>>> etl.stringpatterns(table, 'foo')
+------------+-------+---------------------+
| pattern | count | frequency |
+============+=======+=====================+
| 'Aa. Aaa' | 3 | 0.5 |
+------------+-------+---------------------+
| 'Aaa. Aaa' | 2 | 0.3333333333333333 |
+------------+-------+---------------------+
| 'Aa. Aaaa' | 1 | 0.16666666666666666 |
+------------+-------+---------------------+
>>> etl.stringpatterns(table, 'bar')
+---------------+-------+---------------------+
| pattern | count | frequency |
+===============+=======+=====================+
| '999-9999' | 3 | 0.5 |
+---------------+-------+---------------------+
| '999 9999' | 2 | 0.3333333333333333 |
+---------------+-------+---------------------+
| '999-9999-AA' | 1 | 0.16666666666666666 |
+---------------+-------+---------------------+
"""
counter = stringpatterncounter(table, field)
output = [('pattern', 'count', 'frequency')]
counter = counter.most_common()
total = sum(c[1] for c in counter)
cnts = [(c[0], c[1], float(c[1])/total) for c in counter]
output.extend(cnts)
return wrap(output)
Table.stringpatterns = stringpatterns
def rowlengths(table):
"""
Report on row lengths found in the table. E.g.::
>>> import petl as etl
>>> table = [['foo', 'bar', 'baz'],
... ['A', 1, 2],
... ['B', '2', '3.4'],
... [u'B', u'3', u'7.8', True],
... ['D', 'xyz', 9.0],
... ['E', None],
... ['F', 9]]
>>> etl.rowlengths(table)
+--------+-------+
| length | count |
+========+=======+
| 3 | 3 |
+--------+-------+
| 2 | 2 |
+--------+-------+
| 4 | 1 |
+--------+-------+
Useful for finding potential problems in data files.
"""
counter = Counter()
for row in data(table):
counter[len(row)] += 1
output = [('length', 'count')]
output.extend(counter.most_common())
return wrap(output)
Table.rowlengths = rowlengths