Table package API reference

Table class

class astropy.table.Table(data=None, names=None, dtypes=None, meta=None, copy=True)

A class to represent tables of heterogeneous data.

Table provides a class for heterogeneous tabular data, making use of a numpy structured array internally to store the data values. A key enhancement provided by the Table class is the ability to easily modify the structure of the table by adding or removing columns, or adding new rows of data. In addition table and column metadata are fully supported.

Table differs from NDData by the assumption that the input data consists of columns of homogeneous data, where each column has a unique identifier and may contain additional metadata such as the data units, format, and description.

Parameters :

data : numpy ndarray, dict, list, or Table, optional

Data to initialize table.

names : list, optional

Specify column names

dtypes : list, optional

Specify column data types

meta : dict, optional

Metadata associated with the table

copy : boolean, optional

Copy the input data (default=True).

add_column(col, index=None)

Add a new Column object col to the table. If index is supplied then insert column before index position in the list of columns, otherwise append column to the end of the list.

Parameters :

col : Column

Column object to add.

index : int or None

Insert column before this position or at end (default)

add_columns(cols, indexes=None)

Add a list of new Column objects cols to the table. If a corresponding list of indexes is supplied then insert column before each index position in the original list of columns, otherwise append columns to the end of the list.

Parameters :

cols : list of Columns

Column objects to add.

indexes : list of ints or None

Insert column before this position or at end (default)

add_row(vals=None)

Add a new row to the end of the table.

The vals argument can be:

sequence (e.g. tuple or list)
Column values in the same order as table columns.
mapping (e.g. dict)
Keys corresponding to column names. Missing values will be filled with np.zeros for the column dtype.
None
All values filled with np.zeros for the column dtype.

This method requires that the Table object “owns” the underlying array data. In particular one cannot add a row to a Table that was initialized with copy=False from an existing array.

Parameters :

vals : tuple, list, dict or None

Use the specified values in the new row

field(item)

Return column[item] for recarray compatibility.

index_column(name)

Return the positional index of column name.

Parameters :

name : str

column name

Returns :

index : int

Positional index of column name.

keep_columns(names)

Keep only the columns specified (remove the others)

Parameters :

names : list

A list containing the names of the columns to keep. All other columns will be removed.

next()

Python 3 iterator

remove_column(name)

Remove a column from the table.

This can also be done with:

del table[name]
Parameters :

name : str

Name of column to remove

remove_columns(names)

Remove several columns from the table

Parameters :

names : list

A list containing the names of the columns to remove

rename_column(name, new_name)

Rename a column.

This can also be done directly with by setting the name attribute for a column:

table[name].name = new_name
Parameters :

name : str

The current name of the column.

new_name : str

The new name for the column

reverse()

Reverse the row order of table rows. The table is reversed in place and there are no function arguments.

sort(keys)

Sort the table according to one or more keys. This operates on the existing table and does not return a new table.

Parameters :

keys : str or list of str

The key(s) to order the table by

Column class

class astropy.table.Column

Define a data column for use in a Table object.

Parameters :

name : str

Column name and key for reference within Table

data : list, ndarray or None

Column data values

dtype : numpy.dtype compatible value

Data type for column

shape : tuple or ()

Dimensions of a single row element in the column data

length : int or 0

Number of row elements in column data

description : str or None

Full description of column

units : str or None

Physical units

format : str or None

Format string for outputting column values. This can be an “old-style” (format % value) or “new-style” (str.format) format specification string.

meta : dict-like or None

Meta-data associated with the column

Examples

A Column can be created in two different ways:

  • Provide a data value and optionally a dtype value

    Examples:

    col = Column('name', data=[1, 2, 3])         # shape=(3,)
    col = Column('name', data=[[1, 2], [3, 4]])  # shape=(2, 2)
    col = Column('name', data=[1, 2, 3], dtype=float)
    col = Column('name', np.array([1, 2, 3]))
    col = Column('name', ['hello', 'world'])
    

    The dtype argument can be any value which is an acceptable fixed-size data-type initializer for the numpy.dtype() method. See http://docs.scipy.org/doc/numpy/reference/arrays.dtypes.html. Examples include:

    • Python non-string type (float, int, bool)
    • Numpy non-string type (e.g. np.float32, np.int64, np.bool)
    • Numpy.dtype array-protocol type strings (e.g. ‘i4’, ‘f8’, ‘S15’)

    If no dtype value is provide then the type is inferred using np.array(data). When data is provided then the shape and length arguments are ignored.

  • Provide zero or more of dtype, shape, length

    Examples:

    col = Column('name')
    col = Column('name', dtype=int, length=10, shape=(3,4))
    

    The default dtype is np.float64 and the default length is zero. The shape argument is the array shape of a single cell in the column. The default shape is () which means a single value in each element.

attrs_equal(col)

Compare the column attributes of col to this object.

The comparison attributes are: name, units, dtype, format, description, and meta.

Parameters :

col: Column :

Comparison column

Returns :

equal: boolean :

True if all attributes are equal

copy(data=None, copy_data=True)

Return a copy of the current Column instance.

descr

Array-interface compliant full description of the column.

This returns a 3-tuple (name, type, shape) that can always be used in a structured array dtype definition.

Row class

class astropy.table.Row(table, index)

A class to represent one row of a Table object.

A Row object is returned when a Table object is indexed with an integer or when iterating over a table:

>>> table = Table([(1, 2), (3, 4)], names=('a', 'b'))
>>> row = table[1]
>>> row
<Row 1 of table
 values=(2, 4)
 dtype=[('a', '<i8'), ('b', '<i8')]>
>>> row['a']
2
>>> row[1]
4

TableColumns class

class astropy.table.TableColumns(cols={})

OrderedDict subclass for a set of columns.

This class enhances item access to provide convenient access to columns by name or index, including slice access. It also handles renaming of columns.

The initialization argument cols can be any structure that is valid for initializing a Python dict. This includes a dict, list of (key, val) tuple pairs, list of [key, val] lists, etc.

Parameters :

cols : dict, list, tuple; optional

Column objects as data structure that can init dict (see above)

Project Versions

Table Of Contents

Previous topic

Masking and null values

Next topic

astropy.tools documentation

This Page