Table

class astropy.table.table.Table(data=None, masked=None, names=None, dtypes=None, meta=None, copy=True) [edit on github][source]

Bases: object

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.

masked : boolean, optional

Specify whether the table is masked.

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).

Attributes Summary

ColumnClass
colnames
dtype
mask
masked
meta

Methods Summary

add_column(col[, index]) Add a new Column object col to the table.
add_columns(cols[, indexes]) Add a list of new Column objects cols to the table.
add_row([vals, mask]) Add a new row to the end of the table.
create_mask()
field(item) Return column[item] for recarray compatibility.
filled([fill_value]) Return a copy of self, with masked values filled.
index_column(name) Return the positional index of column name.
keep_columns(names) Keep only the columns specified (remove the others).
keys()
more([max_lines, max_width, show_name, ...]) Interactively browse table with a paging interface.
next() Python 3 iterator
pformat([max_lines, max_width, show_name, ...]) Return a list of lines for the formatted string representation of the table.
pprint([max_lines, max_width, show_name, ...]) Print a formatted string representation of the table.
read(*args, **kwargs) Read in data
remove_column(name) Remove a column from the table.
remove_columns(names) Remove several columns from the table
rename_column(name, new_name) Rename a column.
reverse() Reverse the row order of table rows.
sort(keys) Sort the table according to one or more keys.
write(data, *args, **kwargs) Write out data

Attributes Documentation

ColumnClass[source]
colnames[source]
dtype[source]
mask[source]
masked[source]
meta[source]

Methods Documentation

add_column(col, index=None) [edit on github][source]

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) [edit on github][source]

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, mask=None) [edit on github][source]

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.

The mask attribute should give (if desired) the mask for the values. The type of the mask should match that of the values, i.e. if vals is an iterable, then mask should also be an iterable with the same length, and if vals is a mapping, then mask should be a dictionary.

Parameters :

vals : tuple, list, dict or None

Use the specified values in the new row

create_mask() [edit on github][source]
field(item) [edit on github][source]

Return column[item] for recarray compatibility.

filled(fill_value=None) [edit on github][source]

Return a copy of self, with masked values filled.

If input fill_value supplied then that value is used for all masked entries in the table. Otherwise the individual fill_value defined for each table column is used.

Returns :

filled_table : Table

New table with masked values filled

index_column(name) [edit on github][source]

Return the positional index of column name.

Parameters :

name : str

column name

Returns :

index : int

Positional index of column name.

keep_columns(names) [edit on github][source]

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.

keys() [edit on github][source]
more(max_lines=None, max_width=None, show_name=True, show_units=False) [edit on github][source]

Interactively browse table with a paging interface.

Supported keys:

f, <space> : forward one page
b : back one page
r : refresh same page
n : next row
p : previous row
< : go to beginning
> : go to end
q : quit browsing
h : print this help
Parameters :

max_lines : int

Maximum number of lines in table output

max_width : int or None

Maximum character width of output

show_name : bool

Include a header row for column names (default=True)

show_units : bool

Include a header row for units (default=False)

next() [edit on github]

Python 3 iterator

pformat(max_lines=None, max_width=None, show_name=True, show_units=False, html=False) [edit on github][source]

Return a list of lines for the formatted string representation of the table.

If no value of max_lines is supplied then the height of the screen terminal is used to set max_lines. If the terminal height cannot be determined then the default is taken from the configuration item astropy.table.pprint.MAX_LINES. If a negative value of max_lines is supplied then there is no line limit applied.

The same applies for max_width except the configuration item is astropy.table.pprint.MAX_WIDTH.

Parameters :

max_lines : int or None

Maximum number of rows to output

max_width : int or None

Maximum character width of output

show_name : bool

Include a header row for column names (default=True)

show_units : bool

Include a header row for units (default=False)

html : bool

Format the output as an HTML table (default=False)

Returns :

lines : list

Formatted table as a list of strings

pprint(max_lines=None, max_width=None, show_name=True, show_units=False) [edit on github][source]

Print a formatted string representation of the table.

If no value of max_lines is supplied then the height of the screen terminal is used to set max_lines. If the terminal height cannot be determined then the default is taken from the configuration item astropy.table.pprint.MAX_LINES. If a negative value of max_lines is supplied then there is no line limit applied.

The same applies for max_width except the configuration item is astropy.table.pprint.MAX_WIDTH.

Parameters :

max_lines : int

Maximum number of lines in table output

max_width : int or None

Maximum character width of output

show_name : bool

Include a header row for column names (default=True)

show_units : bool

Include a header row for units (default=False)

classmethod read(*args, **kwargs) [edit on github]

Read in data

The arguments passed to this method depend on the format

remove_column(name) [edit on github][source]

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) [edit on github][source]

Remove several columns from the table

Parameters :

names : list

A list containing the names of the columns to remove

rename_column(name, new_name) [edit on github][source]

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() [edit on github][source]

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

sort(keys) [edit on github][source]

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

write(data, *args, **kwargs) [edit on github]

Write out data

The arguments passed to this method depend on the format

Page Contents