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.

mask : numpy ndarray, dict, list, optional

The mask to initialize the 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).

Attributes Summary

masked
colnames
dtype
ColumnClass
mask

Methods Summary

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

Attributes Documentation

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

Methods Documentation

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

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)

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.

next() [edit on github]

Python 3 iterator

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

Write a table

The arguments passed to this method depend on the format

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

Return column[item] for recarray compatibility.

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)

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

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)

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

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

reverse() [edit on github][source]

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

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

Read a table

The arguments passed to this method depend on the format

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)

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.

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

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

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

Page Contents