def _catalog_filter_table( cls, table: agate.Table, manifest: Manifest ) -> agate.Table: """Filter the table as appropriate for catalog entries. Subclasses can override this to change filtering rules on a per-adapter basis. """ return table.where(_catalog_filter_schemas(manifest))
def test_where_preserves_rows(self): table = Table(self.rows, self.column_names, self.column_types) table2 = table.where(lambda r: r['one'] == 1) table3 = table2.where(lambda r: r['one'] == 1) self.assertIs(table.rows[0], table2.rows[0]) self.assertIs(table2.rows[0], table3.rows[0])
def test_where_preserves_rows(self): table = Table(self.rows, self.columns) table2 = table.where(lambda r: r['one'] == 1) table3 = table2.where(lambda r: r['one'] == 1) self.assertIsNot(table._data[0], table2._data[0]) self.assertIs(table2._data[0], table3._data[0])
def test_where_preserves_rows(self): table = Table(self.rows, self.columns) table2 = table.where(lambda r: r['one'] == 1) table3 = table2.where(lambda r: r['one'] == 1) self.assertIs(table.rows[0], table2.rows[0]) self.assertIs(table2.rows[0], table3.rows[0])
def _catalog_filter_table(cls, table: agate.Table, manifest: Manifest) -> agate.Table: table = table_from_rows( table.rows, table.column_names, text_only_columns=["table_schema", "table_name"], ) return table.where(cls._catalog_filter_schemas(manifest))
def test_where_with_row_names(self): table = Table(self.rows, self.column_names, self.column_types, row_names='three') new_table = table.where(lambda r: r['one'] in (2, None)) self.assertRowNames(new_table, ['b', u'👍'])
def test_where(self): table = Table(self.rows, self.column_names, self.column_types) new_table = table.where(lambda r: r['one'] in (2, None)) self.assertIsNot(new_table, table) self.assertEqual(len(new_table.rows), 2) self.assertSequenceEqual(new_table.rows[0], (2, 3, 'b')) self.assertSequenceEqual(new_table.columns['one'], (2, None))
def test_where(self): table = Table(self.rows, self.columns) new_table = table.where(lambda r: r['one'] in (2, None)) self.assertIsNot(new_table, table) self.assertEqual(len(new_table.rows), 2) self.assertSequenceEqual(new_table.rows[0], (2, 3, 'b')) self.assertSequenceEqual(new_table.columns['one'], (2, None))
def test_where(self): table = Table(self.rows, self.column_names, self.column_types) new_table = table.where(lambda r: r['one'] in (2, None)) self.assertIsNot(new_table, table) self.assertColumnNames(new_table, self.column_names) self.assertColumnTypes(new_table, [Number, Number, Text]) self.assertRows(new_table, [self.rows[1], self.rows[2]])
def _catalog_filter_table(cls, table: agate.Table, manifest: Manifest) -> agate.Table: """Filter the table as appropriate for catalog entries. Subclasses can override this to change filtering rules on a per-adapter basis. """ # force database + schema to be strings table = table_from_rows( table.rows, table.column_names, text_only_columns=['table_database', 'table_schema', 'table_name']) return table.where(_catalog_filter_schemas(manifest))
def test_where(self): table = Table(self.rows, self.column_names, self.column_types) new_table = table.where(lambda r: r['one'] in (2, None)) self.assertIsNot(new_table, table) self.assertColumnNames(new_table, self.column_names) self.assertColumnTypes(new_table, [Number, Number, Text]) self.assertRows(new_table, [ self.rows[1], self.rows[2] ])
def test_where_with_row_names(self): table = Table(self.rows, self.column_names, self.column_types, row_names='three') new_table = table.where(lambda r: r['one'] in (2, None)) self.assertSequenceEqual(table.rows['a'], (1, 4, 'a')) self.assertSequenceEqual(new_table.row_names, ('b', u'👍'))
def test_where_with_row_names(self): table = Table(self.rows, self.columns, row_names='three') new_table = table.where(lambda r: r['one'] in (2, None)) self.assertSequenceEqual(table.rows['a'], (1, 4, 'a')) self.assertSequenceEqual(new_table.row_names, ('b', u'👍'))
('federal_supply_class', text_type), ('federal_supply_class_name', text_type), ) with open('examples/realdata/ks_1033_data.csv') as f: # Create a csv reader 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:')