The Table class (toytable.Table) is the most important component of this package. It is intended to represent a ‘normal’ table - that is one which has no special colums or has not undergone any transformations (e.g. project, expand).
Every Table object has a schema. In it’s simplest form, the schema can be nothing more than a list of string column-names. Specifying a schema this way will produce a non-typed table, in which any Python type can be stored in any column.
Alternativly the schema can include type information. Instead of Specifying the schema item as a string, use (column_name, type), where type is a python type or class object, for example int, str.
It is expected that most of the values stored in the table will be simple objects or native types such as numbers and strings, however it is also possible to store any python object as long as they are hashable.
Parameters: |
|
---|
Bases: object
The basic table class. Table objects contain any Python data type, however some features may be unavailable if the types are non-hashable.
Create a new index on a set of columns.
Indexes are list-like objects which can be used to speed-up access to rows of data. Indexes improve the preformance of operations (e.g. joins).
The Table class only holds a weak-reference to this object, hence the user must retain a reference to the index in order to prevent it from being garbage collected.
Parameters: | cols (List of strings.) – Column names to be included into the index. |
---|
Summarize a table by grouping by one or more keys, and then apply aggregation functions to generate additional summarized columns.
Aggregations are specified as a list of triples in the form: (column name (str), column type (type), column function (callable))
The column function should be a function that returns the type specified in the 2nd column. It’s input will be each of the sub-tables speified by the grouping keys.
>>> from toytable import table_literal
>>> t = table_literal("""
... | Attack(str) | Pokemon(str) | Level Obtained(int) | Attack Type(str) |
... | Thunder Shock | Pikachu | 1 | Electric |
... | Tackle | Pikachu | 1 | Normal |
... | Tail Whip | Pikachu | 1 | Normal |
... | Growl | Pikachu | 5 | Normal |
... | Quick Attack | Pikachu | 10 | Normal |
... | Thunder Wave | Pikachu | 13 | Electric |
... | Electro Ball | Pikachu | 18 | Electric |
... | Charm | Pikachu | 0 | Fairy |
... | Sweet Kiss | Pikachu | 0 | Fairy |
... """)
>>>
>>> agg = t.aggregate(
... keys=('Pokemon', 'Attack Type'),
... aggregations = [
... ('Count', int, lambda t:len(t))
... ]
... )
>>>
>>> print agg
| Pokemon (str) | Attack Type (str) | Count (int) |
| Pikachu | Normal | 4 |
| Pikachu | Electric | 3 |
| Pikachu | Fairy | 2 |
Parameters: |
|
---|
Returns a new DerivedTable in which the named columns have been removed.
Unless the new table is materialised it shares the same data as the table it was made from, hence extending or appending from the new table will also modify the projected table.
Ordering of the original columns will be retained, except that the specified columns will no longer be accessible.
Parameters: | col_names (List of strings) – List of colun names to remove |
---|
Append a single row to this table. The row must match the table’s schema, typically this means that the row should have the same number of items, however if types were specified then the type of each positional element must conform to the required type of the corresponding schema column.
Parameters: | row (List of objects, types must correspond with schema) – A single table row to be added |
---|
Get the table’s column names as a list of strings.
Get the table’s column types as a list of types.
Create a ‘materialised’ copy of this table.
This converts all dynamically generated columns into StaticColumn objects.
Returns a new DerivedTable in which a new calculated column has been added.
This column’s value is determined by a function and a set of input columns.
Parameters: |
|
---|
:param fn; A function or lambda :param type: Optionally, constrain the value of this column by type
Returns a new DerivedTable in which a single column of static data has been added.
Parameters: |
---|
Append all rows in iterable to this table. Each row must conform to this table’s schema. :param iterable: Iterator from which to extract rows :type iterable: iterable
Get a single row from the table. :param key: Row index :type key: int
A convenience function that expands the table with a new hash column.
Left join the other table onto this, return a table.
Parameters: |
|
---|
Left join the other table onto this, return a table.
Parameters: |
|
---|
Returns a new DerivedTable in which only the named columns remain in the order specified by col_names.
Parameters: | col_names (List of strings) – List of column names to keep |
---|
Rename columns in the table. Does not affect the order of columns.
Parameters: |
|
---|
Return a new DerivedTable object in which all visible rows satisfy some kind of logical constraint given by fn.
Parameters: |
|
---|
Get the table’s schema. This is a list of (name (string), type) tuples.
Create a toytable.Table object from a multi-line string expression. The input format is exactly the same as the Table class’s repr format:
>>> from toytable import table_literal
>>> pokedex = table_literal("""
... | Pokemon (str) | Level (int) | Owner (str) | Type (str) |
... | Pikchu | 12 | Ash Ketchum | Electric |
... | Bulbasaur | 16 | Ash Ketchum | Grass |
... | Charmander | 19 | Ash Ketchum | Fire |
... | Raichu | 23 | Lt. Surge | Electric |
... """)
>>>
>>> print pokedex.column_names
['Pokemon', 'Level', 'Owner', 'Type']
>>> print pokedex.column_types
[<type 'str'>, <type 'int'>, <type 'str'>, <type 'str'>]
>>> print list(pokedex.Pokemon)
['Pikchu', 'Bulbasaur', 'Charmander', 'Raichu']
Since table literal expressions are strings all columns require a type to be explicitly specified. All untyped columns are presumed to be strings.
The type column can be any importable object or function capable of building the contents of the column from the string values of each element in the column.
Parameters: |
---|