toyplot.data module

class toyplot.data.Table(data=None, index=False)[source]

Bases: object

Encapsulates an ordered, heterogeneous collection of labelled data series.

Parameters:
  • data ((data series, optional)) – You may initialize a toyplot.data.Table with any of the following:
    • None (the default) - creates an empty table (a table without any columns).
    • toyplot.data.Table - creates a copy of the given table.
    • collections.OrderedDict - creates a column for each key-value pair in the input, in the same order. Each value must be implicitly convertable to a numpy masked array, and every value must contain the same number of items.
    • object returned when loading a .npz file with numpy.load() - creates a column for each key-value pair in the given file, in the same order. Each array in the input file must contain the same number of items.
    • dict / collections.Mapping - creates a column for each key-value pair in the input, sorted by key in lexicographical order. Each value must be implicitly convertable to a numpy masked array, and every value must contain the same number of items.
    • list / collections.Sequence - creates a column for each key-value tuple in the input sequence, in the same order. Each value must be implicitly convertable to a numpy masked array, and every value must contain the same number of items.
    • numpy.ndarray - creates a column for each column in a numpy matrix (2D array). The order of the columns is maintained, and each column is assigned a unique name.
    • pandas.core.frame.DataFrame - creates a column for each column in a Pandas data frame. The order of the columns is maintained.
  • index (bool or string, optional) – Controls whether to convert a Pandas data frame index to columns in the resulting table. Use index=False (the default) to leave the data frame index out of the table. Use index=True to include the index in the table, using default column names (hierarchical indices will be stored in the table using multiple columns). Use index=”format string” to include the index and control how the index column names are generated. The given format string can use positional {} / {0} or keyword {index} arguments to incorporate a zero-based index id into the column names.
items()[source]

Return the table names and columns, in column order.

Returns:items
Return type:sequence of (name, column) tuples.
keys()[source]

Return the table column names, in column order.

Returns:keys
Return type:sequence of str column names.
matrix()[source]

Convert the table to a matrix (2D numpy array).

The data type of the returned array is chosen based on the types of the columns within the table. Tables containing a homogeneous set of column types will return an array of the the same type. If the table contains one or more string columns, the results will be an array of strings.

Returns:matrix
Return type:numpy.ma.core.MaskedArray with two dimensions.
metadata(column)[source]

Return metadata for one of the table’s columns.

Parameters:column (string.) – The name of an existing column.
Returns:metadata
Return type:dict containing key-value pairs.
shape

Return the shape (number of rows and columns) of the table.

Returns:shape
Return type:(number of rows, number of columns) tuple.
values()[source]

Return the table columns, in column order.

Returns:values
Return type:sequence of numpy.ndarray columns.
toyplot.data.contiguous(a)[source]

Split an array into a collection of contiguous ranges.

toyplot.data.minimax(items)[source]

Compute the minimum and maximum of an arbitrary collection of scalar- or array-like items.

The items parameter must be an iterable containing any combination of None, scalars, numpy arrays, or numpy masked arrays. None, NaN, masked values, and empty arrays are all handled correctly. Returns (None, None) if the inputs don’t contain any usable values.

Returns:
  • min (minimum value of the input arrays, or None.)
  • max (maximum value of the input arrays, or None.)
toyplot.data.read_csv(fobj, convert=False)[source]

Load a CSV (delimited text) file.

Parameters:
  • fobj (file-like object or string, required) – The file to read. Use a string filepath, an open file, or a file-like object.
  • convert (boolean, optional) – By default, the columns in a table will contain strings. If True, convert column types to integers and floats where possible.
Returns:

table

Return type:

toyplot.data.Table

Notes

read_csv() is a simple tool for use in demos and tutorials. For more full-featured delimited text parsing, you should consider the csv module included in the Python standard library, or functionality provided by numpy or Pandas.