def test_group_by_group_name(self): table = Table(self.rows, self.columns) tableset = table.group_by('one', key_name='test') self.assertIsInstance(tableset, TableSet) self.assertEqual(tableset._key_name, 'test')
def test_group_by_function_group_name(self): table = Table(self.rows, self.columns) tableset = table.group_by(lambda r: r['three'] < 5, key_name='test') self.assertIsInstance(tableset, TableSet) self.assertEqual(tableset._key_name, 'test')
def test_group_by_function(self): table = Table(self.rows, self.columns) new_tables = table.group_by(lambda r: r['three'] < 5) self.assertIsInstance(new_tables, TableSet) self.assertEqual(len(new_tables), 2) self.assertIn('True', new_tables.keys()) self.assertIn('False', new_tables.keys()) self.assertSequenceEqual(new_tables['True'].columns['one'], ('a', 'a', 'b')) self.assertSequenceEqual(new_tables['False'].columns['one'], (None,))
def test_group_by_function(self): table = Table(self.rows, self.columns) new_tables = table.group_by(lambda r: r['three'] < 5) self.assertIsInstance(new_tables, TableSet) self.assertEqual(len(new_tables), 2) self.assertIn('True', new_tables.keys()) self.assertIn('False', new_tables.keys()) self.assertSequenceEqual(new_tables['True'].columns['one'], ('a', 'a', 'b')) self.assertSequenceEqual(new_tables['False'].columns['one'], (None, ))
def test_group_by_function(self): table = Table(self.rows, self.columns) tableset = table.group_by(lambda r: r['three'] < 5) self.assertIsInstance(tableset, TableSet) self.assertEqual(len(tableset), 2) self.assertEqual(tableset._key_name, 'group') self.assertIn('True', tableset.keys()) self.assertIn('False', tableset.keys()) self.assertSequenceEqual(tableset['True'].columns['one'], ('a', 'a', 'b')) self.assertSequenceEqual(tableset['False'].columns['one'], (None,))
def test_group_by_function(self): table = Table(self.rows, self.columns) tableset = table.group_by(lambda r: r['three'] < 5, key_type=Boolean()) self.assertIsInstance(tableset, TableSet) self.assertEqual(len(tableset), 2) self.assertEqual(tableset.key_name, 'group') self.assertIn(True, tableset.keys()) self.assertIn(False, tableset.keys()) self.assertSequenceEqual(tableset[True].columns['one'], ('a', 'a', 'b')) self.assertSequenceEqual(tableset[False].columns['one'], (None,))
def test_group_by_key_type(self): table = Table(self.rows, self.columns) tableset = table.group_by('two', key_type=Text()) self.assertIsInstance(tableset, TableSet) self.assertEqual(tableset.key_name, 'two') self.assertIsInstance(tableset.key_type, Text) self.assertIn('2', tableset.keys()) self.assertIn('3', tableset.keys()) self.assertSequenceEqual(tableset['2'].columns['one'], ('a', 'a')) self.assertSequenceEqual(tableset['3'].columns['one'], (None, 'b'))
def test_group_by_function(self): table = Table(self.rows, self.column_names, self.column_types) tableset = table.group_by(lambda r: r['three'] < 5, key_type=Boolean()) self.assertIsInstance(tableset, TableSet) self.assertEqual(len(tableset), 2) self.assertEqual(tableset.key_name, 'group') self.assertIn(True, tableset.keys()) self.assertIn(False, tableset.keys()) self.assertSequenceEqual(tableset[True].columns['one'], ('a', 'a', 'b')) self.assertSequenceEqual(tableset[False].columns['one'], (None,))
def test_group_by_key_type(self): table = Table(self.rows, self.column_names, self.column_types) tableset = table.group_by('two', key_type=Text()) self.assertIsInstance(tableset, TableSet) self.assertEqual(tableset.key_name, 'two') self.assertIsInstance(tableset.key_type, Text) self.assertIn('2', tableset.keys()) self.assertIn('3', tableset.keys()) self.assertSequenceEqual(tableset['2'].columns['one'], ('a', 'a')) self.assertSequenceEqual(tableset['3'].columns['one'], (None, 'b'))
def test_group_by_key_name(self): table = Table(self.rows, self.columns) tableset = table.group_by('one', key_name='test') self.assertIsInstance(tableset, TableSet) self.assertEqual(tableset.key_name, 'test') self.assertIsInstance(tableset.key_type, Text) self.assertIn('a', tableset.keys()) self.assertIn('b', tableset.keys()) self.assertIn(None, tableset.keys()) self.assertSequenceEqual(tableset['a'].columns['one'], ('a', 'a')) self.assertSequenceEqual(tableset['b'].columns['one'], ('b',))
def test_group_by_number(self): table = Table(self.rows, self.columns) tableset = table.group_by('two') self.assertIsInstance(tableset, TableSet) self.assertEqual(len(tableset), 2) self.assertEqual(tableset.key_name, 'two') self.assertIsInstance(tableset.key_type, Number) self.assertIn(Decimal('2'), tableset.keys()) self.assertIn(Decimal('3'), tableset.keys()) self.assertSequenceEqual(tableset[Decimal('2')].columns['one'], ('a', 'a')) self.assertSequenceEqual(tableset[Decimal('3')].columns['one'], (None, 'b'))
def test_group_by(self): table = Table(self.rows, self.columns) new_tables = table.group_by('one') self.assertIsInstance(new_tables, TableSet) self.assertEqual(len(new_tables), 3) self.assertIn('a', new_tables.keys()) self.assertIn('b', new_tables.keys()) self.assertIn('None', new_tables.keys()) self.assertSequenceEqual(new_tables['a'].columns['one'], ('a', 'a')) self.assertSequenceEqual(new_tables['b'].columns['one'], ('b', )) self.assertSequenceEqual(new_tables['None'].columns['one'], (None, ))
def test_group_by(self): table = Table(self.rows, self.columns) new_tables = table.group_by('one') self.assertIsInstance(new_tables, TableSet) self.assertEqual(len(new_tables), 3) self.assertIn('a', new_tables.keys()) self.assertIn('b', new_tables.keys()) self.assertIn('None', new_tables.keys()) self.assertSequenceEqual(new_tables['a'].columns['one'], ('a', 'a')) self.assertSequenceEqual(new_tables['b'].columns['one'], ('b',)) self.assertSequenceEqual(new_tables['None'].columns['one'], (None,))
def test_group_by_number(self): table = Table(self.rows, self.column_names, self.column_types) tableset = table.group_by('two') self.assertIsInstance(tableset, TableSet) self.assertEqual(len(tableset), 2) self.assertEqual(tableset.key_name, 'two') self.assertIsInstance(tableset.key_type, Number) self.assertIn(Decimal('2'), tableset.keys()) self.assertIn(Decimal('3'), tableset.keys()) self.assertSequenceEqual(tableset[Decimal('2')].columns['one'], ('a', 'a')) self.assertSequenceEqual(tableset[Decimal('3')].columns['one'], (None, 'b'))
def test_group_by(self): table = Table(self.rows, self.columns) tableset = table.group_by('one') self.assertIsInstance(tableset, TableSet) self.assertEqual(len(tableset), 3) self.assertEqual(tableset._key_name, 'one') self.assertIn('a', tableset.keys()) self.assertIn('b', tableset.keys()) self.assertIn('None', tableset.keys()) self.assertSequenceEqual(tableset['a'].columns['one'], ('a', 'a')) self.assertSequenceEqual(tableset['b'].columns['one'], ('b', )) self.assertSequenceEqual(tableset['None'].columns['one'], (None, ))
def test_group_by(self): table = Table(self.rows, self.columns) tableset = table.group_by('one') self.assertIsInstance(tableset, TableSet) self.assertEqual(len(tableset), 3) self.assertEqual(tableset._key_name, 'one') self.assertIn('a', tableset.keys()) self.assertIn('b', tableset.keys()) self.assertIn('None', tableset.keys()) self.assertSequenceEqual(tableset['a'].columns['one'], ('a', 'a')) self.assertSequenceEqual(tableset['b'].columns['one'], ('b',)) self.assertSequenceEqual(tableset['None'].columns['one'], (None,))
def test_group_by_bad_column(self): table = Table(self.rows, self.column_names, self.column_types) with self.assertRaises(KeyError): table.group_by('bad')
reader = csv.reader(f) # Skip header next(f) # Create the table table = Table(reader, COLUMNS) # Filter to counties containing Kansas City kansas_city = table.where(lambda r: r['county'] in ('JACKSON', 'CLAY', 'CASS', 'PLATTE')) # Sum total_cost of four counties print('Total for Kansas City area: %i' % kansas_city.columns['total_cost'].aggregate(Sum())) # Group by county counties = table.group_by('county') # Aggregate totals for all counties totals = counties.aggregate([ ('total_cost', Sum(), 'total_cost_sum') ]) totals = totals.order_by('total_cost_sum', reverse=True).rows[:5] print('Five most spendy counties:') for i, row in enumerate(totals): text = '# {}: {}, ${:,}'.format(i + 1, row['group'], row['total_cost_sum']) print(text) # Get the five most recent purchases
def test_group_by_bad_column(self): table = Table(self.rows, self.columns) with self.assertRaises(ColumnDoesNotExistError): table.group_by('bad')
def test_group_by_bad_column(self): table = Table(self.rows, self.columns) with self.assertRaises(KeyError): table.group_by('bad')