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
|
from decimal import Decimal
from babel.numbers import get_decimal_symbol
from agate import Table
from agate.data_types import Number, Text
from agate.testcase import AgateTestCase
class TestBins(AgateTestCase):
def setUp(self):
self.number_type = Number()
self.column_names = ['number']
self.column_types = [self.number_type]
def test_bins(self):
rows = []
for i in range(0, 100):
rows.append([i]),
new_table = Table(rows, self.column_names, self.column_types).bins('number')
self.assertColumnNames(new_table, ['number', 'Count'])
self.assertColumnTypes(new_table, [Text, Number])
self.assertSequenceEqual(new_table.rows[0], ['[0 - 10)', 10])
self.assertSequenceEqual(new_table.rows[3], ['[30 - 40)', 10])
self.assertSequenceEqual(new_table.rows[9], ['[90 - 100]', 10])
self.assertRowNames(new_table, [
'[0 - 10)',
'[10 - 20)',
'[20 - 30)',
'[30 - 40)',
'[40 - 50)',
'[50 - 60)',
'[60 - 70)',
'[70 - 80)',
'[80 - 90)',
'[90 - 100]',
])
def test_bins_negative(self):
rows = []
for i in range(0, -100, -1):
rows.append([i])
new_table = Table(rows, self.column_names, self.column_types).bins('number', 10, start=-100)
self.assertColumnNames(new_table, ['number', 'Count'])
self.assertColumnTypes(new_table, [Text, Number])
self.assertSequenceEqual(new_table.rows[0], ['[-100 - -90)', 9])
self.assertSequenceEqual(new_table.rows[3], ['[-70 - -60)', 10])
self.assertSequenceEqual(new_table.rows[9], ['[-10 - 0]', 11])
def test_bins_mixed_signs(self):
rows = []
for i in range(0, -100, -1):
rows.append([i + 50])
new_table = Table(rows, self.column_names, self.column_types).bins('number')
self.assertColumnNames(new_table, ['number', 'Count'])
self.assertColumnTypes(new_table, [Text, Number])
self.assertSequenceEqual(new_table.rows[0], ['[-50 - -40)', 9])
self.assertSequenceEqual(new_table.rows[3], ['[-20 - -10)', 10])
self.assertSequenceEqual(new_table.rows[9], ['[40 - 50]', 11])
def test_bins_small_numbers(self):
rows = []
for i in range(0, 100):
rows.append([Decimal(i) / Decimal('10')])
new_table = Table(rows, self.column_names, self.column_types).bins('number')
self.assertSequenceEqual(new_table.rows[0], ['[0 - 1)', 10])
self.assertSequenceEqual(new_table.rows[3], ['[3 - 4)', 10])
self.assertSequenceEqual(new_table.rows[9], ['[9 - 10]', 10])
def test_bins_values_outside_start_end(self):
rows = []
for i in range(0, 100):
rows.append([Decimal(i) / Decimal('10')])
table_one = Table(rows, self.column_names, self.column_types).bins('number', start=1, end=11)
table_two = Table(rows, self.column_names, self.column_types).bins('number', start=-1, end=9)
self.assertSequenceEqual(table_one.rows[0], ['[0 - 2)', 20])
self.assertSequenceEqual(table_two.rows[8], ['[8 - 10]', 20])
def test_bins_decimals(self):
rows = []
for i in range(0, 100):
rows.append([Decimal(i) / Decimal('100')])
new_table = Table(rows, self.column_names, self.column_types).bins('number')
self.assertColumnNames(new_table, ['number', 'Count'])
self.assertColumnTypes(new_table, [Text, Number])
self.assertSequenceEqual(
new_table.rows[0],
['[0' + get_decimal_symbol() + '0 - 0' + get_decimal_symbol() + '1)', 10]
)
self.assertSequenceEqual(
new_table.rows[3],
['[0' + get_decimal_symbol() + '3 - 0' + get_decimal_symbol() + '4)', 10]
)
self.assertSequenceEqual(
new_table.rows[9],
['[0' + get_decimal_symbol() + '9 - 1' + get_decimal_symbol() + '0]', 10]
)
def test_bins_nulls(self):
rows = []
for i in range(0, 100):
rows.append([Decimal(i) / Decimal('100')])
rows.append([None])
new_table = Table(rows, self.column_names, self.column_types).bins('number')
self.assertColumnNames(new_table, ['number', 'Count'])
self.assertColumnTypes(new_table, [Text, Number])
self.assertSequenceEqual(
new_table.rows[0],
['[0' + get_decimal_symbol() + '0 - 0' + get_decimal_symbol() + '1)', 10]
)
self.assertSequenceEqual(
new_table.rows[3],
['[0' + get_decimal_symbol() + '3 - 0' + get_decimal_symbol() + '4)', 10]
)
self.assertSequenceEqual(
new_table.rows[9],
['[0' + get_decimal_symbol() + '9 - 1' + get_decimal_symbol() + '0]', 10]
)
self.assertSequenceEqual(new_table.rows[10], [None, 1])
|