Table

class gwpy.table.Table(data=None, masked=None, names=None, dtype=None, meta=None, copy=True, rows=None, copy_indices=True, **kwargs)

Bases: object

A class to represent tables of heterogeneous data.

Table provides a class for heterogeneous tabular data. A key enhancement provided by the Table class over e.g. a numpy structured array 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 unit, format, and description.

See also: http://docs.astropy.org/en/stable/table/

Parameters

data : numpy ndarray, dict, list, Table, or table-like object, optional

Data to initialize table.

masked : bool, optional

Specify whether the table is masked.

names : list, optional

Specify column names.

dtype : list, optional

Specify column data types.

meta : dict, optional

Metadata associated with the table.

copy : bool, optional

Copy the input data. If the input is a Table the meta is always copied regardless of the copy parameter. Default is True.

rows : numpy ndarray, list of lists, optional

Row-oriented data for table instead of data argument.

copy_indices : bool, optional

Copy any indices in the input data. Default is True.

**kwargs : dict, optional

Additional keyword args when converting table-like object.

Attributes Summary

ColumnClass

colnames

dtype

groups

has_mixin_columns

True if table has any mixin columns (defined as columns that are not Column subclasses).

iloc

Return a TableILoc object that can be used for retrieving indexed rows in the order they appear in the index.

indices

Return the indices associated with columns of the table as a TableIndices object.

info([option, out])

loc

Return a TableLoc object that can be used for retrieving rows by index in a given data range.

loc_indices

Return a TableLocIndices object that can be used for retrieving the row indices corresponding to given table index key value or values.

mask

masked

meta

read(\*args, \*\*kwargs)

write(\*args, \*\*kwargs)

Methods Summary

add_column(self, col[, index, name, …])

Add a new Column object col to the table.

add_columns(self, cols[, indexes, names, …])

Add a list of new Column objects cols to the table.

add_index(self, colnames[, engine, unique])

Insert a new index among one or more columns.

add_row(self[, vals, mask])

Add a new row to the end of the table.

argsort(self[, keys, kind, reverse])

Return the indices which would sort the table according to one or more key columns.

as_array(self[, keep_byteorder, names])

Return a new copy of the table in the form of a structured np.ndarray or np.ma.MaskedArray object (as appropriate).

convert_bytestring_to_unicode(self)

Convert bytestring columns (dtype.kind=’S’) to unicode (dtype.kind=’U’) using UTF-8 encoding.

convert_unicode_to_bytestring(self)

Convert unicode columns (dtype.kind=’U’) to bytestring (dtype.kind=’S’) using UTF-8 encoding.

copy(self[, copy_data])

Return a copy of the table.

field(self, item)

Return column[item] for recarray compatibility.

filled(self[, fill_value])

Return copy of self, with masked values filled.

from_pandas(dataframe[, index])

Create a Table from a pandas.DataFrame instance

group_by(self, keys)

Group this table by the specified keys

index_column(self, name)

Return the positional index of column name.

index_mode(self, mode)

Return a context manager for an indexing mode.

insert_row(self, index[, vals, mask])

Add a new row before the given index position in the table.

itercols(self)

Iterate over the columns of this table.

keep_columns(self, names)

Keep only the columns specified (remove the others).

keys(self)

more(self[, max_lines, max_width, …])

Interactively browse table with a paging interface.

pformat(self[, max_lines, max_width, …])

Return a list of lines for the formatted string representation of

pformat_all(self[, max_lines, max_width, …])

Return a list of lines for the formatted string representation of

pprint(self[, max_lines, max_width, …])

Print a formatted string representation of the table.

pprint_all(self[, max_lines, max_width, …])

Print a formatted string representation of the entire table.

remove_column(self, name)

Remove a column from the table.

remove_columns(self, names)

Remove several columns from the table.

remove_indices(self, colname)

Remove all indices involving the given column.

remove_row(self, index)

Remove a row from the table.

remove_rows(self, row_specifier)

Remove rows from the table.

rename_column(self, name, new_name)

Rename a column.

rename_columns(self, names, new_names)

Rename multiple columns.

replace_column(self, name, col)

Replace column name with the new col object.

reverse(self)

Reverse the row order of table rows.

show_in_browser(self[, max_lines, jsviewer, …])

Render the table in HTML and show it in a web browser.

show_in_notebook(self[, tableid, css, …])

Render the table in HTML and show it in the IPython notebook.

sort(self[, keys, reverse])

Sort the table according to one or more keys.

to_pandas(self[, index])

Return a pandas.DataFrame instance

Attributes Documentation

ColumnClass
colnames
dtype
groups
has_mixin_columns

True if table has any mixin columns (defined as columns that are not Column subclasses).

iloc

Return a TableILoc object that can be used for retrieving indexed rows in the order they appear in the index.

indices

Return the indices associated with columns of the table as a TableIndices object.

info(option='attributes', out='')
loc

Return a TableLoc object that can be used for retrieving rows by index in a given data range. Note that both loc and iloc work only with single-column indices.

loc_indices

Return a TableLocIndices object that can be used for retrieving the row indices corresponding to given table index key value or values.

mask
masked
meta
read(*args, **kwargs) = <astropy.table.connect.TableRead object>
write(*args, **kwargs) = <astropy.table.connect.TableWrite object>

Methods Documentation

add_column(self, col, index=None, name=None, rename_duplicate=False, copy=True)

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

name : str

Column name

rename_duplicate : bool

Uniquify column name if it already exist. Default is False.

copy : bool

Make a copy of the new column. Default is True.

Examples

Create a table with two columns ‘a’ and ‘b’:

>>> t = Table([[1, 2, 3], [0.1, 0.2, 0.3]], names=('a', 'b'))
>>> print(t)
 a   b
--- ---
  1 0.1
  2 0.2
  3 0.3

Create a third column ‘c’ and append it to the end of the table:

>>> col_c = Column(name='c', data=['x', 'y', 'z'])
>>> t.add_column(col_c)
>>> print(t)
 a   b   c
--- --- ---
  1 0.1   x
  2 0.2   y
  3 0.3   z

Add column ‘d’ at position 1. Note that the column is inserted before the given index:

>>> col_d = Column(name='d', data=['a', 'b', 'c'])
>>> t.add_column(col_d, 1)
>>> print(t)
 a   d   b   c
--- --- --- ---
  1   a 0.1   x
  2   b 0.2   y
  3   c 0.3   z

Add second column named ‘b’ with rename_duplicate:

>>> t = Table([[1, 2, 3], [0.1, 0.2, 0.3]], names=('a', 'b'))
>>> col_b = Column(name='b', data=[1.1, 1.2, 1.3])
>>> t.add_column(col_b, rename_duplicate=True)
>>> print(t)
 a   b  b_1
--- --- ---
  1 0.1 1.1
  2 0.2 1.2
  3 0.3 1.3

Add an unnamed column or mixin object in the table using a default name or by specifying an explicit name with name. Name can also be overridden:

>>> t = Table([[1, 2], [0.1, 0.2]], names=('a', 'b'))
>>> col_c = Column(data=['x', 'y'])
>>> t.add_column(col_c)
>>> t.add_column(col_c, name='c')
>>> col_b = Column(name='b', data=[1.1, 1.2])
>>> t.add_column(col_b, name='d')
>>> print(t)
 a   b  col2  c   d
--- --- ---- --- ---
  1 0.1    x   x 1.1
  2 0.2    y   y 1.2

To add several columns use add_columns.

add_columns(self, cols, indexes=None, names=None, copy=True, rename_duplicate=False)

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

names : list of str

Column names

copy : bool

Make a copy of the new columns. Default is True.

rename_duplicate : bool

Uniquify new column names if they duplicate the existing ones. Default is False.

Examples

Create a table with two columns ‘a’ and ‘b’:

>>> t = Table([[1, 2, 3], [0.1, 0.2, 0.3]], names=('a', 'b'))
>>> print(t)
 a   b
--- ---
  1 0.1
  2 0.2
  3 0.3

Create column ‘c’ and ‘d’ and append them to the end of the table:

>>> col_c = Column(name='c', data=['x', 'y', 'z'])
>>> col_d = Column(name='d', data=['u', 'v', 'w'])
>>> t.add_columns([col_c, col_d])
>>> print(t)
 a   b   c   d
--- --- --- ---
  1 0.1   x   u
  2 0.2   y   v
  3 0.3   z   w

Add column ‘c’ at position 0 and column ‘d’ at position 1. Note that the columns are inserted before the given position:

>>> t = Table([[1, 2, 3], [0.1, 0.2, 0.3]], names=('a', 'b'))
>>> col_c = Column(name='c', data=['x', 'y', 'z'])
>>> col_d = Column(name='d', data=['u', 'v', 'w'])
>>> t.add_columns([col_c, col_d], [0, 1])
>>> print(t)
 c   a   d   b
--- --- --- ---
  x   1   u 0.1
  y   2   v 0.2
  z   3   w 0.3

Add second column ‘b’ and column ‘c’ with rename_duplicate:

>>> t = Table([[1, 2, 3], [0.1, 0.2, 0.3]], names=('a', 'b'))
>>> col_b = Column(name='b', data=[1.1, 1.2, 1.3])
>>> col_c = Column(name='c', data=['x', 'y', 'z'])
>>> t.add_columns([col_b, col_c], rename_duplicate=True)
>>> print(t)
 a   b  b_1  c
--- --- --- ---
  1 0.1 1.1  x
  2 0.2 1.2  y
  3 0.3 1.3  z

Add unnamed columns or mixin objects in the table using default names or by specifying explicit names with names. Names can also be overridden:

>>> t = Table()
>>> col_a = Column(data=['x', 'y'])
>>> col_b = Column(name='b', data=['u', 'v'])
>>> t.add_columns([col_a, col_b])
>>> t.add_columns([col_a, col_b], names=['c', 'd'])
>>> print(t)
col0  b   c   d
---- --- --- ---
   x   u   x   u
   y   v   y   v
add_index(self, colnames, engine=None, unique=False)

Insert a new index among one or more columns. If there are no indices, make this index the primary table index.

Parameters

colnames : str or list

List of column names (or a single column name) to index

engine : type or None

Indexing engine class to use, from among SortedArray, BST, FastBST, FastRBT, and SCEngine. If the supplied argument is None (by default), use SortedArray.

unique : bool

Whether the values of the index must be unique. Default is False.

add_row(self, vals=None, mask=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.

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

mask : tuple, list, dict or None

Use the specified mask values in the new row

Examples

Create a table with three columns ‘a’, ‘b’ and ‘c’:

>>> t = Table([[1,2],[4,5],[7,8]], names=('a','b','c'))
>>> print(t)
 a   b   c
--- --- ---
  1   4   7
  2   5   8

Adding a new row with entries ‘3’ in ‘a’, ‘6’ in ‘b’ and ‘9’ in ‘c’:

>>> t.add_row([3,6,9])
>>> print(t)
  a   b   c
  --- --- ---
  1   4   7
  2   5   8
  3   6   9
argsort(self, keys=None, kind=None, reverse=False)

Return the indices which would sort the table according to one or more key columns. This simply calls the numpy.argsort function on the table with the order parameter set to keys.

Parameters

keys : str or list of str

The column name(s) to order the table by

kind : {‘quicksort’, ‘mergesort’, ‘heapsort’}, optional

Sorting algorithm.

reverse : bool

Sort in reverse order (default=False)

Returns

index_array : ndarray, int

Array of indices that sorts the table by the specified key column(s).

as_array(self, keep_byteorder=False, names=None)

Return a new copy of the table in the form of a structured np.ndarray or np.ma.MaskedArray object (as appropriate).

Parameters

keep_byteorder : bool, optional

By default the returned array has all columns in native byte order. However, if this option is True this preserves the byte order of all columns (if any are non-native).

names : list, optional:

List of column names to include for returned structured array. Default is to include all table columns.

Returns

table_array : np.ndarray (unmasked) or np.ma.MaskedArray (masked)

Copy of table as a numpy structured array

convert_bytestring_to_unicode(self)

Convert bytestring columns (dtype.kind=’S’) to unicode (dtype.kind=’U’) using UTF-8 encoding.

Internally this changes string columns to represent each character in the string with a 4-byte UCS-4 equivalent, so it is inefficient for memory but allows scripts to manipulate string arrays with natural syntax.

convert_unicode_to_bytestring(self)

Convert unicode columns (dtype.kind=’U’) to bytestring (dtype.kind=’S’) using UTF-8 encoding.

When exporting a unicode string array to a file, it may be desirable to encode unicode columns as bytestrings.

copy(self, copy_data=True)

Return a copy of the table.

Parameters

copy_data : bool

If True (the default), copy the underlying data array. Otherwise, use the same data array. The meta is always deepcopied regardless of the value for copy_data.

field(self, item)

Return column[item] for recarray compatibility.

filled(self, fill_value=None)

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

Parameters

fill_value : str

If supplied, this fill_value is used for all masked entries in the entire table.

Returns

filled_table : Table

New table with masked values filled

classmethod from_pandas(dataframe, index=False)

Create a Table from a pandas.DataFrame instance

In addition to converting generic numeric or string columns, this supports conversion of pandas Date and Time delta columns to Time and TimeDelta columns, respectively.

Parameters

dataframe : pandas.DataFrame

A pandas pandas.DataFrame instance

index : bool

Include the index column in the returned table (default=False)

Returns

table : Table

A Table (or subclass) instance

Raises

ImportError

If pandas is not installed

Examples

Here we convert a pandas.DataFrame instance to a QTable.

>>> import numpy as np
>>> import pandas as pd
>>> from astropy.table import QTable
>>> time = pd.Series(['1998-01-01', '2002-01-01'], dtype='datetime64[ns]')
>>> dt = pd.Series(np.array([1, 300], dtype='timedelta64[s]'))
>>> df = pd.DataFrame({'time': time})
>>> df['dt'] = dt
>>> df['x'] = [3., 4.]
>>> with pd.option_context('display.max_columns', 20):
...     print(df)
        time       dt    x
0 1998-01-01 00:00:01  3.0
1 2002-01-01 00:05:00  4.0
>>> QTable.from_pandas(df)
<QTable length=2>
          time            dt      x
         object         object float64
----------------------- ------ -------
1998-01-01T00:00:00.000    1.0     3.0
2002-01-01T00:00:00.000  300.0     4.0
group_by(self, keys)

Group this table by the specified keys

This effectively splits the table into groups which correspond to unique values of the keys grouping object. The output is a new TableGroups which contains a copy of this table but sorted by row according to keys.

The keys input to group_by can be specified in different ways:

  • String or list of strings corresponding to table column name(s)

  • Numpy array (homogeneous or structured) with same length as this table

  • Table with same length as this table

Parameters

keys : str, list of str, numpy array, or Table

Key grouping object

Returns

out : Table

New table with groups set

index_column(self, name)

Return the positional index of column name.

Parameters

name : str

column name

Returns

index : int

Positional index of column name.

Examples

Create a table with three columns ‘a’, ‘b’ and ‘c’:

>>> t = Table([[1, 2, 3], [0.1, 0.2, 0.3], ['x', 'y', 'z']],
...           names=('a', 'b', 'c'))
>>> print(t)
 a   b   c
--- --- ---
  1 0.1   x
  2 0.2   y
  3 0.3   z

Get index of column ‘b’ of the table:

>>> t.index_column('b')
1
index_mode(self, mode)

Return a context manager for an indexing mode.

Parameters

mode : str

Either ‘freeze’, ‘copy_on_getitem’, or ‘discard_on_copy’. In ‘discard_on_copy’ mode, indices are not copied whenever columns or tables are copied. In ‘freeze’ mode, indices are not modified whenever columns are modified; at the exit of the context, indices refresh themselves based on column values. This mode is intended for scenarios in which one intends to make many additions or modifications in an indexed column. In ‘copy_on_getitem’ mode, indices are copied when taking column slices as well as table slices, so col[i0:i1] will preserve indices.

insert_row(self, index, vals=None, mask=None)

Add a new row before the given index position in 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.

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

mask : tuple, list, dict or None

Use the specified mask values in the new row

itercols(self)

Iterate over the columns of this table.

Examples

To iterate over the columns of a table:

>>> t = Table([[1], [2]])
>>> for col in t.itercols():
...     print(col)
col0
----
   1
col1
----
   2

Using itercols() is similar to for col in t.columns.values() but is syntactically preferred.

keep_columns(self, 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.

Examples

Create a table with three columns ‘a’, ‘b’ and ‘c’:

>>> t = Table([[1, 2, 3],[0.1, 0.2, 0.3],['x', 'y', 'z']],
...           names=('a', 'b', 'c'))
>>> print(t)
 a   b   c
--- --- ---
  1 0.1   x
  2 0.2   y
  3 0.3   z

Specifying only a single column name keeps only this column. Keep only column ‘a’ of the table:

>>> t.keep_columns('a')
>>> print(t)
 a
---
  1
  2
  3

Specifying a list of column names is keeps is also possible. Keep columns ‘a’ and ‘c’ of the table:

>>> t = Table([[1, 2, 3],[0.1, 0.2, 0.3],['x', 'y', 'z']],
...           names=('a', 'b', 'c'))
>>> t.keep_columns(['a', 'c'])
>>> print(t)
 a   c
--- ---
  1   x
  2   y
  3   z
keys(self)
more(self, max_lines=None, max_width=None, show_name=True, show_unit=None, show_dtype=False)

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 is True.

show_unit : bool

Include a header row for unit. Default is to show a row for units only if one or more columns has a defined value for the unit.

show_dtype : bool

Include a header row for column dtypes. Default is True.

pformat(self, max_lines=None, max_width=None, show_name=True, show_unit=None, show_dtype=False, html=False, tableid=None, align=None, tableclass=None)
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.conf.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.conf.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 is True.

show_unit : bool

Include a header row for unit. Default is to show a row for units only if one or more columns has a defined value for the unit.

show_dtype : bool

Include a header row for column dtypes. Default is True.

html : bool

Format the output as an HTML table. Default is False.

tableid : str or None

An ID tag for the table; only used if html is set. Default is “table{id}”, where id is the unique integer id of the table object, id(self)

align : str or list or tuple or None

Left/right alignment of columns. Default is right (None) for all columns. Other allowed values are ‘>’, ‘<’, ‘^’, and ‘0=’ for right, left, centered, and 0-padded, respectively. A list of strings can be provided for alignment of tables with multiple columns.

tableclass : str or list of str or None

CSS classes for the table; only used if html is set. Default is None.

Returns

lines : list

Formatted table as a list of strings.

pformat_all(self, max_lines=-1, max_width=-1, show_name=True, show_unit=None, show_dtype=False, html=False, tableid=None, align=None, tableclass=None)
Return a list of lines for the formatted string representation of

the entire 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.conf.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.conf.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 is True.

show_unit : bool

Include a header row for unit. Default is to show a row for units only if one or more columns has a defined value for the unit.

show_dtype : bool

Include a header row for column dtypes. Default is True.

html : bool

Format the output as an HTML table. Default is False.

tableid : str or None

An ID tag for the table; only used if html is set. Default is “table{id}”, where id is the unique integer id of the table object, id(self)

align : str or list or tuple or None

Left/right alignment of columns. Default is right (None) for all columns. Other allowed values are ‘>’, ‘<’, ‘^’, and ‘0=’ for right, left, centered, and 0-padded, respectively. A list of strings can be provided for alignment of tables with multiple columns.

tableclass : str or list of str or None

CSS classes for the table; only used if html is set. Default is None.

Returns

lines : list

Formatted table as a list of strings.

pprint(self, max_lines=None, max_width=None, show_name=True, show_unit=None, show_dtype=False, align=None)

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.conf.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.conf.max_width.

Parameters

max_lines : int or None

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 is True.

show_unit : bool

Include a header row for unit. Default is to show a row for units only if one or more columns has a defined value for the unit.

show_dtype : bool

Include a header row for column dtypes. Default is True.

align : str or list or tuple or None

Left/right alignment of columns. Default is right (None) for all columns. Other allowed values are ‘>’, ‘<’, ‘^’, and ‘0=’ for right, left, centered, and 0-padded, respectively. A list of strings can be provided for alignment of tables with multiple columns.

pprint_all(self, max_lines=-1, max_width=-1, show_name=True, show_unit=None, show_dtype=False, align=None)

Print a formatted string representation of the entire table.

This method is the same as astropy.table.Table.pprint except that the default max_lines and max_width are both -1 so that by default the entire table is printed instead of restricting to the size of the screen terminal.

Parameters

max_lines : int or None

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 is True.

show_unit : bool

Include a header row for unit. Default is to show a row for units only if one or more columns has a defined value for the unit.

show_dtype : bool

Include a header row for column dtypes. Default is True.

align : str or list or tuple or None

Left/right alignment of columns. Default is right (None) for all columns. Other allowed values are ‘>’, ‘<’, ‘^’, and ‘0=’ for right, left, centered, and 0-padded, respectively. A list of strings can be provided for alignment of tables with multiple columns.

remove_column(self, name)

Remove a column from the table.

This can also be done with:

del table[name]
Parameters

name : str

Name of column to remove

Examples

Create a table with three columns ‘a’, ‘b’ and ‘c’:

>>> t = Table([[1, 2, 3], [0.1, 0.2, 0.3], ['x', 'y', 'z']],
...           names=('a', 'b', 'c'))
>>> print(t)
 a   b   c
--- --- ---
  1 0.1   x
  2 0.2   y
  3 0.3   z

Remove column ‘b’ from the table:

>>> t.remove_column('b')
>>> print(t)
 a   c
--- ---
  1   x
  2   y
  3   z

To remove several columns at the same time use remove_columns.

remove_columns(self, names)

Remove several columns from the table.

Parameters

names : list

A list containing the names of the columns to remove

Examples

Create a table with three columns ‘a’, ‘b’ and ‘c’:

>>> t = Table([[1, 2, 3], [0.1, 0.2, 0.3], ['x', 'y', 'z']],
...     names=('a', 'b', 'c'))
>>> print(t)
 a   b   c
--- --- ---
  1 0.1   x
  2 0.2   y
  3 0.3   z

Remove columns ‘b’ and ‘c’ from the table:

>>> t.remove_columns(['b', 'c'])
>>> print(t)
 a
---
  1
  2
  3

Specifying only a single column also works. Remove column ‘b’ from the table:

>>> t = Table([[1, 2, 3], [0.1, 0.2, 0.3], ['x', 'y', 'z']],
...     names=('a', 'b', 'c'))
>>> t.remove_columns('b')
>>> print(t)
 a   c
--- ---
  1   x
  2   y
  3   z

This gives the same as using remove_column.

remove_indices(self, colname)

Remove all indices involving the given column. If the primary index is removed, the new primary index will be the most recently added remaining index.

Parameters

colname : str

Name of column

remove_row(self, index)

Remove a row from the table.

Parameters

index : int

Index of row to remove

Examples

Create a table with three columns ‘a’, ‘b’ and ‘c’:

>>> t = Table([[1, 2, 3], [0.1, 0.2, 0.3], ['x', 'y', 'z']],
...           names=('a', 'b', 'c'))
>>> print(t)
 a   b   c
--- --- ---
  1 0.1   x
  2 0.2   y
  3 0.3   z

Remove row 1 from the table:

>>> t.remove_row(1)
>>> print(t)
 a   b   c
--- --- ---
  1 0.1   x
  3 0.3   z

To remove several rows at the same time use remove_rows.

remove_rows(self, row_specifier)

Remove rows from the table.

Parameters

row_specifier : slice, int, or array of ints

Specification for rows to remove

Examples

Create a table with three columns ‘a’, ‘b’ and ‘c’:

>>> t = Table([[1, 2, 3], [0.1, 0.2, 0.3], ['x', 'y', 'z']],
...           names=('a', 'b', 'c'))
>>> print(t)
 a   b   c
--- --- ---
  1 0.1   x
  2 0.2   y
  3 0.3   z

Remove rows 0 and 2 from the table:

>>> t.remove_rows([0, 2])
>>> print(t)
 a   b   c
--- --- ---
  2 0.2   y

Note that there are no warnings if the slice operator extends outside the data:

>>> t = Table([[1, 2, 3], [0.1, 0.2, 0.3], ['x', 'y', 'z']],
...           names=('a', 'b', 'c'))
>>> t.remove_rows(slice(10, 20, 1))
>>> print(t)
 a   b   c
--- --- ---
  1 0.1   x
  2 0.2   y
  3 0.3   z
rename_column(self, 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

TODO: this won’t work for mixins

Parameters

name : str

The current name of the column.

new_name : str

The new name for the column

Examples

Create a table with three columns ‘a’, ‘b’ and ‘c’:

>>> t = Table([[1,2],[3,4],[5,6]], names=('a','b','c'))
>>> print(t)
 a   b   c
--- --- ---
  1   3   5
  2   4   6

Renaming column ‘a’ to ‘aa’:

>>> t.rename_column('a' , 'aa')
>>> print(t)
 aa  b   c
--- --- ---
  1   3   5
  2   4   6
rename_columns(self, names, new_names)

Rename multiple columns.

Parameters

names : list, tuple

A list or tuple of existing column names.

new_names : list, tuple

A list or tuple of new column names.

Examples

Create a table with three columns ‘a’, ‘b’, ‘c’:

>>> t = Table([[1,2],[3,4],[5,6]], names=('a','b','c'))
>>> print(t)
  a   b   c
 --- --- ---
  1   3   5
  2   4   6

Renaming columns ‘a’ to ‘aa’ and ‘b’ to ‘bb’:

>>> names = ('a','b')
>>> new_names = ('aa','bb')
>>> t.rename_columns(names, new_names)
>>> print(t)
 aa  bb   c
--- --- ---
  1   3   5
  2   4   6
replace_column(self, name, col)

Replace column name with the new col object.

Parameters

name : str

Name of column to replace

col : column object (list, ndarray, Column, etc)

New column object to replace the existing column

Examples

Replace column ‘a’ with a float version of itself:

>>> t = Table([[1, 2, 3], [0.1, 0.2, 0.3]], names=('a', 'b'))
>>> float_a = t['a'].astype(float)
>>> t.replace_column('a', float_a)
reverse(self)

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

Examples

Create a table with three columns:

>>> t = Table([['Max', 'Jo', 'John'], ['Miller','Miller','Jackson'],
...         [12,15,18]], names=('firstname','name','tel'))
>>> print(t)
firstname   name  tel
--------- ------- ---
      Max  Miller  12
       Jo  Miller  15
     John Jackson  18

Reversing order:

>>> t.reverse()
>>> print(t)
firstname   name  tel
--------- ------- ---
     John Jackson  18
       Jo  Miller  15
      Max  Miller  12
show_in_browser(self, max_lines=5000, jsviewer=False, browser='default', jskwargs={'use_local_files': True}, tableid=None, table_class='display compact', css=None, show_row_index='idx')

Render the table in HTML and show it in a web browser.

Parameters

max_lines : int

Maximum number of rows to export to the table (set low by default to avoid memory issues, since the browser view requires duplicating the table in memory). A negative value of max_lines indicates no row limit.

jsviewer : bool

If True, prepends some javascript headers so that the table is rendered as a DataTables data table. This allows in-browser searching & sorting.

browser : str

Any legal browser name, e.g. 'firefox', 'chrome', 'safari' (for mac, you may need to use 'open -a "/Applications/Google Chrome.app" {}' for Chrome). If 'default', will use the system default browser.

jskwargs : dict

Passed to the astropy.table.JSViewer init. Defaults to {'use_local_files': True} which means that the JavaScript libraries will be served from local copies.

tableid : str or None

An html ID tag for the table. Default is table{id}, where id is the unique integer id of the table object, id(self).

table_class : str or None

A string with a list of HTML classes used to style the table. Default is “display compact”, and other possible values can be found in https://www.datatables.net/manual/styling/classes

css : string

A valid CSS string declaring the formatting for the table. Defaults to astropy.table.jsviewer.DEFAULT_CSS.

show_row_index : str or False

If this does not evaluate to False, a column with the given name will be added to the version of the table that gets displayed. This new column shows the index of the row in the table itself, even when the displayed table is re-sorted by another column. Note that if a column with this name already exists, this option will be ignored. Defaults to “idx”.

show_in_notebook(self, tableid=None, css=None, display_length=50, table_class='astropy-default', show_row_index='idx')

Render the table in HTML and show it in the IPython notebook.

Parameters

tableid : str or None

An html ID tag for the table. Default is table{id}-XXX, where id is the unique integer id of the table object, id(self), and XXX is a random number to avoid conflicts when printing the same table multiple times.

table_class : str or None

A string with a list of HTML classes used to style the table. The special default string (‘astropy-default’) means that the string will be retrieved from the configuration item astropy.table.default_notebook_table_class. Note that these table classes may make use of bootstrap, as this is loaded with the notebook. See this page for the list of classes.

css : string

A valid CSS string declaring the formatting for the table. Defaults to astropy.table.jsviewer.DEFAULT_CSS_NB.

display_length : int, optional

Number or rows to show. Defaults to 50.

show_row_index : str or False

If this does not evaluate to False, a column with the given name will be added to the version of the table that gets displayed. This new column shows the index of the row in the table itself, even when the displayed table is re-sorted by another column. Note that if a column with this name already exists, this option will be ignored. Defaults to “idx”.

Notes

Currently, unlike show_in_browser (with jsviewer=True), this method needs to access online javascript code repositories. This is due to modern browsers’ limitations on accessing local files. Hence, if you call this method while offline (and don’t have a cached version of jquery and jquery.dataTables), you will not get the jsviewer features.

sort(self, keys=None, reverse=False)

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. If None, use the primary index of the Table.

reverse : bool

Sort in reverse order (default=False)

Examples

Create a table with 3 columns:

>>> t = Table([['Max', 'Jo', 'John'], ['Miller', 'Miller', 'Jackson'],
...            [12, 15, 18]], names=('firstname', 'name', 'tel'))
>>> print(t)
firstname   name  tel
--------- ------- ---
      Max  Miller  12
       Jo  Miller  15
     John Jackson  18

Sorting according to standard sorting rules, first ‘name’ then ‘firstname’:

>>> t.sort(['name', 'firstname'])
>>> print(t)
firstname   name  tel
--------- ------- ---
     John Jackson  18
       Jo  Miller  15
      Max  Miller  12

Sorting according to standard sorting rules, first ‘firstname’ then ‘tel’, in reverse order:

>>> t.sort(['firstname', 'tel'], reverse=True)
>>> print(t)
firstname   name  tel
--------- ------- ---
      Max  Miller  12
     John Jackson  18
       Jo  Miller  15
to_pandas(self, index=None)

Return a pandas.DataFrame instance

The index of the created DataFrame is controlled by the index argument. For index=True or the default None, an index will be specified for the DataFrame if there is a primary key index on the Table and if it corresponds to a single column. If index=False then no DataFrame index will be specified. If index is the name of a column in the table then that will be the DataFrame index.

In additional to vanilla columns or masked columns, this supports Table mixin columns like Quantity, Time, or SkyCoord. In many cases these objects have no analog in pandas and will be converted to a “encoded” representation using only Column or MaskedColumn. The exception is Time or TimeDelta columns, which will be converted to the corresponding representation in pandas using np.datetime64 or np.timedelta64. See the example below.

Returns

dataframe : pandas.DataFrame

A pandas pandas.DataFrame instance

index : None, bool, str

Specify DataFrame index mode

Raises

ImportError

If pandas is not installed

ValueError

If the Table has multi-dimensional columns

Examples

Here we convert a table with a few mixins to a pandas.DataFrame instance.

>>> import pandas as pd
>>> from astropy.table import QTable
>>> import astropy.units as u
>>> from astropy.time import Time, TimeDelta
>>> from astropy.coordinates import SkyCoord
>>> q = [1, 2] * u.m
>>> tm = Time([1998, 2002], format='jyear')
>>> sc = SkyCoord([5, 6], [7, 8], unit='deg')
>>> dt = TimeDelta([3, 200] * u.s)
>>> t = QTable([q, tm, sc, dt], names=['q', 'tm', 'sc', 'dt'])
>>> df = t.to_pandas(index='tm')
>>> with pd.option_context('display.max_columns', 20):
...     print(df)
              q  sc.ra  sc.dec       dt
tm
1998-01-01  1.0    5.0     7.0 00:00:03
2002-01-01  2.0    6.0     8.0 00:03:20
class Column

Bases: astropy.table.column.BaseColumn

Define a data column for use in a Table object.

Parameters

data : list, ndarray or None

Column data values

name : str

Column name and key for reference within Table

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

unit : str or None

Physical unit

format : str or None or function or callable

Format string for outputting column values. This can be an “old-style” (format % value) or “new-style” (str.format) format specification string or a function or any callable object that accepts a single value and returns a 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 but not shape or length (which are inferred from the data).

    Examples:

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

    The dtype argument can be any value which is an acceptable fixed-size data-type initializer for the numpy.dtype() method. See https://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).

  • Provide length and optionally shape, but not data

    Examples:

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

    The default dtype is np.float64. The shape argument is the array shape of a single cell in the column.

convert_unit_to(self, new_unit, equivalencies=[])

Converts the values of the column in-place from the current unit to the given unit.

To change the unit associated with this column without actually changing the data values, simply set the unit property.

Parameters

new_unit : str or astropy.units.UnitBase instance

The unit to convert to.

equivalencies : list of equivalence pairs, optional

A list of equivalence pairs to try if the unit are not directly convertible. See Equivalencies.

Raises

astropy.units.UnitsError

If units are inconsistent

copy(self, order='C', data=None, copy_data=True)

Return a copy of the current instance.

If data is supplied then a view (reference) of data is used, and copy_data is ignored.

Parameters

order : {‘C’, ‘F’, ‘A’, ‘K’}, optional

Controls the memory layout of the copy. ‘C’ means C-order, ‘F’ means F-order, ‘A’ means ‘F’ if a is Fortran contiguous, ‘C’ otherwise. ‘K’ means match the layout of a as closely as possible. (Note that this function and :func:numpy.copy are very similar, but have different default values for their order= arguments.) Default is ‘C’.

data : array, optional

If supplied then use a view of data instead of the instance data. This allows copying the instance attributes and meta.

copy_data : bool, optional

Make a copy of the internal numpy array instead of using a reference. Default is True.

Returns

col : Column or MaskedColumn

Copy of the current column (same type as original)

insert(self, obj, values, axis=0)

Insert values before the given indices in the column and return a new Column object.

Parameters

obj : int, slice or sequence of ints

Object that defines the index or indices before which values is inserted.

values : array_like

Value(s) to insert. If the type of values is different from that of quantity, values is converted to the matching type. values should be shaped so that it can be broadcast appropriately

axis : int, optional

Axis along which to insert values. If axis is None then the column array is flattened before insertion. Default is 0, which will insert a row.

Returns

out : Column

A copy of column with values and mask inserted. Note that the insertion does not occur in-place: a new column is returned.

more(self, max_lines=None, show_name=True, show_unit=False)

Interactively browse column 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.

show_name : bool

Include a header row for column names. Default is True.

show_unit : bool

Include a header row for unit. Default is False.

property name

The name of this column.

pformat(self, max_lines=None, show_name=True, show_unit=False, show_dtype=False, html=False)

Return a list of formatted string representation of column values.

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 will be determined using the astropy.conf.max_lines configuration item. If a negative value of max_lines is supplied then there is no line limit applied.

Parameters

max_lines : int

Maximum lines of output (header + data rows)

show_name : bool

Include column name. Default is True.

show_unit : bool

Include a header row for unit. Default is False.

show_dtype : bool

Include column dtype. Default is False.

html : bool

Format the output as an HTML table. Default is False.

Returns

lines : list

List of lines with header and formatted column values

pprint(self, max_lines=None, show_name=True, show_unit=False, show_dtype=False)

Print a formatted string representation of column values.

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 will be determined using the astropy.conf.max_lines configuration item. If a negative value of max_lines is supplied then there is no line limit applied.

Parameters

max_lines : int

Maximum number of values in output

show_name : bool

Include column name. Default is True.

show_unit : bool

Include a header row for unit. Default is False.

show_dtype : bool

Include column dtype. Default is True.

property quantity

A view of this table column as a Quantity object with units given by the Column’s unit parameter.

to(self, unit, equivalencies=[], **kwargs)

Converts this table column to a Quantity object with the requested units.

Parameters

unit : Unit or str

The unit to convert to (i.e., a valid argument to the astropy.units.Quantity.to() method).

equivalencies : list of equivalence pairs, optional

Equivalencies to use for this conversion. See astropy.units.Quantity.to() for more details.

Returns

quantity : Quantity

A quantity object with the contents of this column in the units unit.

property unit

The unit associated with this column. May be a string or a astropy.units.UnitBase instance.

Setting the unit property does not change the values of the data. To perform a unit conversion, use convert_unit_to.

class MaskedColumn

Bases: astropy.table.column.Column, astropy.table._column_mixins._MaskedColumnGetitemShim, numpy.ma.core.MaskedArray

Define a masked data column for use in a Table object.

Parameters

data : list, ndarray or None

Column data values

name : str

Column name and key for reference within Table

mask : list, ndarray or None

Boolean mask for which True indicates missing or invalid data

fill_value : float, int, str or None

Value used when filling masked column elements

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

unit : str or None

Physical unit

format : str or None or function or callable

Format string for outputting column values. This can be an “old-style” (format % value) or “new-style” (str.format) format specification string or a function or any callable object that accepts a single value and returns a string.

meta : dict-like or None

Meta-data associated with the column

Examples

A MaskedColumn is similar to a Column except that it includes mask and fill_value attributes. It can be created in two different ways:

  • Provide a data value but not shape or length (which are inferred from the data).

    Examples:

    col = MaskedColumn(data=[1, 2], name='name')
    col = MaskedColumn(data=[1, 2], name='name', mask=[True, False])
    col = MaskedColumn(data=[1, 2], name='name', dtype=float, fill_value=99)
    

    The mask argument will be cast as a boolean array and specifies which elements are considered to be missing or invalid.

    The dtype argument can be any value which is an acceptable fixed-size data-type initializer for the numpy.dtype() method. See https://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 length and optionally shape, but not data

    Examples:

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

    The default dtype is np.float64. The shape argument is the array shape of a single cell in the column.

convert_unit_to(self, new_unit, equivalencies=[])

Converts the values of the column in-place from the current unit to the given unit.

To change the unit associated with this column without actually changing the data values, simply set the unit property.

Parameters

new_unit : str or astropy.units.UnitBase instance

The unit to convert to.

equivalencies : list of equivalence pairs, optional

A list of equivalence pairs to try if the unit are not directly convertible. See Equivalencies.

Raises

astropy.units.UnitsError

If units are inconsistent

copy(self, order='C', data=None, copy_data=True)

Return a copy of the current instance.

If data is supplied then a view (reference) of data is used, and copy_data is ignored.

Parameters

order : {‘C’, ‘F’, ‘A’, ‘K’}, optional

Controls the memory layout of the copy. ‘C’ means C-order, ‘F’ means F-order, ‘A’ means ‘F’ if a is Fortran contiguous, ‘C’ otherwise. ‘K’ means match the layout of a as closely as possible. (Note that this function and :func:numpy.copy are very similar, but have different default values for their order= arguments.) Default is ‘C’.

data : array, optional

If supplied then use a view of data instead of the instance data. This allows copying the instance attributes and meta.

copy_data : bool, optional

Make a copy of the internal numpy array instead of using a reference. Default is True.

Returns

col : Column or MaskedColumn

Copy of the current column (same type as original)

property data

Returns the underlying data, as a view of the masked array.

If the underlying data is a subclass of numpy.ndarray, it is returned as such.

>>> x = np.ma.array(np.matrix([[1, 2], [3, 4]]), mask=[[0, 1], [1, 0]])
>>> x.data
matrix([[1, 2],
        [3, 4]])

The type of the data can be accessed through the baseclass attribute.

property fill_value

The filling value of the masked array is a scalar. When setting, None will set to a default based on the data type.

Examples

>>> for dt in [np.int32, np.int64, np.float64, np.complex128]:
...     np.ma.array([0, 1], dtype=dt).get_fill_value()
...
999999
999999
1e+20
(1e+20+0j)
>>> x = np.ma.array([0, 1.], fill_value=-np.inf)
>>> x.fill_value
-inf
>>> x.fill_value = np.pi
>>> x.fill_value
3.1415926535897931 # may vary

Reset to default:

>>> x.fill_value = None
>>> x.fill_value
1e+20
filled(self, fill_value=None)

Return a copy of self, with masked values filled with a given value.

Parameters

fill_value : scalar; optional

The value to use for invalid entries (None by default). If None, the fill_value attribute of the array is used instead.

Returns

filled_column : Column

A copy of self with masked entries replaced by fill_value (be it the function argument or the attribute of self).

insert(self, obj, values, mask=None, axis=0)

Insert values along the given axis before the given indices and return a new MaskedColumn object.

Parameters

obj : int, slice or sequence of ints

Object that defines the index or indices before which values is inserted.

values : array_like

Value(s) to insert. If the type of values is different from that of quantity, values is converted to the matching type. values should be shaped so that it can be broadcast appropriately

mask : boolean array_like

Mask value(s) to insert. If not supplied then False is used.

axis : int, optional

Axis along which to insert values. If axis is None then the column array is flattened before insertion. Default is 0, which will insert a row.

Returns

out : MaskedColumn

A copy of column with values and mask inserted. Note that the insertion does not occur in-place: a new masked column is returned.

more(self, max_lines=None, show_name=True, show_unit=False)

Interactively browse column 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.

show_name : bool

Include a header row for column names. Default is True.

show_unit : bool

Include a header row for unit. Default is False.

property name

The name of this column.

pformat(self, max_lines=None, show_name=True, show_unit=False, show_dtype=False, html=False)

Return a list of formatted string representation of column values.

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 will be determined using the astropy.conf.max_lines configuration item. If a negative value of max_lines is supplied then there is no line limit applied.

Parameters

max_lines : int

Maximum lines of output (header + data rows)

show_name : bool

Include column name. Default is True.

show_unit : bool

Include a header row for unit. Default is False.

show_dtype : bool

Include column dtype. Default is False.

html : bool

Format the output as an HTML table. Default is False.

Returns

lines : list

List of lines with header and formatted column values

pprint(self, max_lines=None, show_name=True, show_unit=False, show_dtype=False)

Print a formatted string representation of column values.

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 will be determined using the astropy.conf.max_lines configuration item. If a negative value of max_lines is supplied then there is no line limit applied.

Parameters

max_lines : int

Maximum number of values in output

show_name : bool

Include column name. Default is True.

show_unit : bool

Include a header row for unit. Default is False.

show_dtype : bool

Include column dtype. Default is True.

class Row(table, index)

Bases: object

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:

>>> from astropy.table import Table
>>> table = Table([(1, 2), (3, 4)], names=('a', 'b'),
...               dtype=('int32', 'int32'))
>>> row = table[1]
>>> row
<Row index=1>
  a     b
int32 int32
----- -----
    2     4
>>> row['a']
2
>>> row[1]
4
as_void(self)

Returns a read-only copy of the row values in the form of np.void or np.ma.mvoid objects. This corresponds to the object types returned for row indexing of a pure numpy structured array or masked array. This method is slow and its use is discouraged when possible.

Returns

void_row : np.void (unmasked) or np.ma.mvoid (masked)

Copy of row values

class TableColumns(cols={})

Bases: collections.OrderedDict

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 a list of Column objects or any structure that is valid for initializing a Python dict. This includes a dict, list of (key, val) tuples or [key, val] lists, etc.

Parameters

cols : dict, list, tuple; optional

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

isinstance(self, cls)

Return a list of columns which are instances of the specified classes.

Parameters

cls : class or tuple of classes

Column class (including mixin) or tuple of Column classes.

Returns

col_list : list of Columns

List of Column objects which are instances of given classes.

keys(self)
not_isinstance(self, cls)

Return a list of columns which are not instances of the specified classes.

Parameters

cls : class or tuple of classes

Column class (including mixin) or tuple of Column classes.

Returns

col_list : list of Columns

List of Column objects which are not instances of given classes.

values(self)
add_column(self, col, index=None, name=None, rename_duplicate=False, copy=True)

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

name : str

Column name

rename_duplicate : bool

Uniquify column name if it already exist. Default is False.

copy : bool

Make a copy of the new column. Default is True.

Examples

Create a table with two columns ‘a’ and ‘b’:

>>> t = Table([[1, 2, 3], [0.1, 0.2, 0.3]], names=('a', 'b'))
>>> print(t)
 a   b
--- ---
  1 0.1
  2 0.2
  3 0.3

Create a third column ‘c’ and append it to the end of the table:

>>> col_c = Column(name='c', data=['x', 'y', 'z'])
>>> t.add_column(col_c)
>>> print(t)
 a   b   c
--- --- ---
  1 0.1   x
  2 0.2   y
  3 0.3   z

Add column ‘d’ at position 1. Note that the column is inserted before the given index:

>>> col_d = Column(name='d', data=['a', 'b', 'c'])
>>> t.add_column(col_d, 1)
>>> print(t)
 a   d   b   c
--- --- --- ---
  1   a 0.1   x
  2   b 0.2   y
  3   c 0.3   z

Add second column named ‘b’ with rename_duplicate:

>>> t = Table([[1, 2, 3], [0.1, 0.2, 0.3]], names=('a', 'b'))
>>> col_b = Column(name='b', data=[1.1, 1.2, 1.3])
>>> t.add_column(col_b, rename_duplicate=True)
>>> print(t)
 a   b  b_1
--- --- ---
  1 0.1 1.1
  2 0.2 1.2
  3 0.3 1.3

Add an unnamed column or mixin object in the table using a default name or by specifying an explicit name with name. Name can also be overridden:

>>> t = Table([[1, 2], [0.1, 0.2]], names=('a', 'b'))
>>> col_c = Column(data=['x', 'y'])
>>> t.add_column(col_c)
>>> t.add_column(col_c, name='c')
>>> col_b = Column(name='b', data=[1.1, 1.2])
>>> t.add_column(col_b, name='d')
>>> print(t)
 a   b  col2  c   d
--- --- ---- --- ---
  1 0.1    x   x 1.1
  2 0.2    y   y 1.2

To add several columns use add_columns.

add_columns(self, cols, indexes=None, names=None, copy=True, rename_duplicate=False)

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

names : list of str

Column names

copy : bool

Make a copy of the new columns. Default is True.

rename_duplicate : bool

Uniquify new column names if they duplicate the existing ones. Default is False.

Examples

Create a table with two columns ‘a’ and ‘b’:

>>> t = Table([[1, 2, 3], [0.1, 0.2, 0.3]], names=('a', 'b'))
>>> print(t)
 a   b
--- ---
  1 0.1
  2 0.2
  3 0.3

Create column ‘c’ and ‘d’ and append them to the end of the table:

>>> col_c = Column(name='c', data=['x', 'y', 'z'])
>>> col_d = Column(name='d', data=['u', 'v', 'w'])
>>> t.add_columns([col_c, col_d])
>>> print(t)
 a   b   c   d
--- --- --- ---
  1 0.1   x   u
  2 0.2   y   v
  3 0.3   z   w

Add column ‘c’ at position 0 and column ‘d’ at position 1. Note that the columns are inserted before the given position:

>>> t = Table([[1, 2, 3], [0.1, 0.2, 0.3]], names=('a', 'b'))
>>> col_c = Column(name='c', data=['x', 'y', 'z'])
>>> col_d = Column(name='d', data=['u', 'v', 'w'])
>>> t.add_columns([col_c, col_d], [0, 1])
>>> print(t)
 c   a   d   b
--- --- --- ---
  x   1   u 0.1
  y   2   v 0.2
  z   3   w 0.3

Add second column ‘b’ and column ‘c’ with rename_duplicate:

>>> t = Table([[1, 2, 3], [0.1, 0.2, 0.3]], names=('a', 'b'))
>>> col_b = Column(name='b', data=[1.1, 1.2, 1.3])
>>> col_c = Column(name='c', data=['x', 'y', 'z'])
>>> t.add_columns([col_b, col_c], rename_duplicate=True)
>>> print(t)
 a   b  b_1  c
--- --- --- ---
  1 0.1 1.1  x
  2 0.2 1.2  y
  3 0.3 1.3  z

Add unnamed columns or mixin objects in the table using default names or by specifying explicit names with names. Names can also be overridden:

>>> t = Table()
>>> col_a = Column(data=['x', 'y'])
>>> col_b = Column(name='b', data=['u', 'v'])
>>> t.add_columns([col_a, col_b])
>>> t.add_columns([col_a, col_b], names=['c', 'd'])
>>> print(t)
col0  b   c   d
---- --- --- ---
   x   u   x   u
   y   v   y   v
add_index(self, colnames, engine=None, unique=False)

Insert a new index among one or more columns. If there are no indices, make this index the primary table index.

Parameters

colnames : str or list

List of column names (or a single column name) to index

engine : type or None

Indexing engine class to use, from among SortedArray, BST, FastBST, FastRBT, and SCEngine. If the supplied argument is None (by default), use SortedArray.

unique : bool

Whether the values of the index must be unique. Default is False.

add_row(self, vals=None, mask=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.

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

mask : tuple, list, dict or None

Use the specified mask values in the new row

Examples

Create a table with three columns ‘a’, ‘b’ and ‘c’:

>>> t = Table([[1,2],[4,5],[7,8]], names=('a','b','c'))
>>> print(t)
 a   b   c
--- --- ---
  1   4   7
  2   5   8

Adding a new row with entries ‘3’ in ‘a’, ‘6’ in ‘b’ and ‘9’ in ‘c’:

>>> t.add_row([3,6,9])
>>> print(t)
  a   b   c
  --- --- ---
  1   4   7
  2   5   8
  3   6   9
argsort(self, keys=None, kind=None, reverse=False)

Return the indices which would sort the table according to one or more key columns. This simply calls the numpy.argsort function on the table with the order parameter set to keys.

Parameters

keys : str or list of str

The column name(s) to order the table by

kind : {‘quicksort’, ‘mergesort’, ‘heapsort’}, optional

Sorting algorithm.

reverse : bool

Sort in reverse order (default=False)

Returns

index_array : ndarray, int

Array of indices that sorts the table by the specified key column(s).

as_array(self, keep_byteorder=False, names=None)

Return a new copy of the table in the form of a structured np.ndarray or np.ma.MaskedArray object (as appropriate).

Parameters

keep_byteorder : bool, optional

By default the returned array has all columns in native byte order. However, if this option is True this preserves the byte order of all columns (if any are non-native).

names : list, optional:

List of column names to include for returned structured array. Default is to include all table columns.

Returns

table_array : np.ndarray (unmasked) or np.ma.MaskedArray (masked)

Copy of table as a numpy structured array

convert_bytestring_to_unicode(self)

Convert bytestring columns (dtype.kind=’S’) to unicode (dtype.kind=’U’) using UTF-8 encoding.

Internally this changes string columns to represent each character in the string with a 4-byte UCS-4 equivalent, so it is inefficient for memory but allows scripts to manipulate string arrays with natural syntax.

convert_unicode_to_bytestring(self)

Convert unicode columns (dtype.kind=’U’) to bytestring (dtype.kind=’S’) using UTF-8 encoding.

When exporting a unicode string array to a file, it may be desirable to encode unicode columns as bytestrings.

copy(self, copy_data=True)

Return a copy of the table.

Parameters

copy_data : bool

If True (the default), copy the underlying data array. Otherwise, use the same data array. The meta is always deepcopied regardless of the value for copy_data.

field(self, item)

Return column[item] for recarray compatibility.

filled(self, fill_value=None)

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

Parameters

fill_value : str

If supplied, this fill_value is used for all masked entries in the entire table.

Returns

filled_table : Table

New table with masked values filled

classmethod from_pandas(dataframe, index=False)

Create a Table from a pandas.DataFrame instance

In addition to converting generic numeric or string columns, this supports conversion of pandas Date and Time delta columns to Time and TimeDelta columns, respectively.

Parameters

dataframe : pandas.DataFrame

A pandas pandas.DataFrame instance

index : bool

Include the index column in the returned table (default=False)

Returns

table : Table

A Table (or subclass) instance

Raises

ImportError

If pandas is not installed

Examples

Here we convert a pandas.DataFrame instance to a QTable.

>>> import numpy as np
>>> import pandas as pd
>>> from astropy.table import QTable
>>> time = pd.Series(['1998-01-01', '2002-01-01'], dtype='datetime64[ns]')
>>> dt = pd.Series(np.array([1, 300], dtype='timedelta64[s]'))
>>> df = pd.DataFrame({'time': time})
>>> df['dt'] = dt
>>> df['x'] = [3., 4.]
>>> with pd.option_context('display.max_columns', 20):
...     print(df)
        time       dt    x
0 1998-01-01 00:00:01  3.0
1 2002-01-01 00:05:00  4.0
>>> QTable.from_pandas(df)
<QTable length=2>
          time            dt      x
         object         object float64
----------------------- ------ -------
1998-01-01T00:00:00.000    1.0     3.0
2002-01-01T00:00:00.000  300.0     4.0
group_by(self, keys)

Group this table by the specified keys

This effectively splits the table into groups which correspond to unique values of the keys grouping object. The output is a new TableGroups which contains a copy of this table but sorted by row according to keys.

The keys input to group_by can be specified in different ways:

  • String or list of strings corresponding to table column name(s)

  • Numpy array (homogeneous or structured) with same length as this table

  • Table with same length as this table

Parameters

keys : str, list of str, numpy array, or Table

Key grouping object

Returns

out : Table

New table with groups set

property has_mixin_columns

True if table has any mixin columns (defined as columns that are not Column subclasses).

property iloc

Return a TableILoc object that can be used for retrieving indexed rows in the order they appear in the index.

index_column(self, name)

Return the positional index of column name.

Parameters

name : str

column name

Returns

index : int

Positional index of column name.

Examples

Create a table with three columns ‘a’, ‘b’ and ‘c’:

>>> t = Table([[1, 2, 3], [0.1, 0.2, 0.3], ['x', 'y', 'z']],
...           names=('a', 'b', 'c'))
>>> print(t)
 a   b   c
--- --- ---
  1 0.1   x
  2 0.2   y
  3 0.3   z

Get index of column ‘b’ of the table:

>>> t.index_column('b')
1
index_mode(self, mode)

Return a context manager for an indexing mode.

Parameters

mode : str

Either ‘freeze’, ‘copy_on_getitem’, or ‘discard_on_copy’. In ‘discard_on_copy’ mode, indices are not copied whenever columns or tables are copied. In ‘freeze’ mode, indices are not modified whenever columns are modified; at the exit of the context, indices refresh themselves based on column values. This mode is intended for scenarios in which one intends to make many additions or modifications in an indexed column. In ‘copy_on_getitem’ mode, indices are copied when taking column slices as well as table slices, so col[i0:i1] will preserve indices.

property indices

Return the indices associated with columns of the table as a TableIndices object.

insert_row(self, index, vals=None, mask=None)

Add a new row before the given index position in 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.

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

mask : tuple, list, dict or None

Use the specified mask values in the new row

itercols(self)

Iterate over the columns of this table.

Examples

To iterate over the columns of a table:

>>> t = Table([[1], [2]])
>>> for col in t.itercols():
...     print(col)
col0
----
   1
col1
----
   2

Using itercols() is similar to for col in t.columns.values() but is syntactically preferred.

keep_columns(self, 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.

Examples

Create a table with three columns ‘a’, ‘b’ and ‘c’:

>>> t = Table([[1, 2, 3],[0.1, 0.2, 0.3],['x', 'y', 'z']],
...           names=('a', 'b', 'c'))
>>> print(t)
 a   b   c
--- --- ---
  1 0.1   x
  2 0.2   y
  3 0.3   z

Specifying only a single column name keeps only this column. Keep only column ‘a’ of the table:

>>> t.keep_columns('a')
>>> print(t)
 a
---
  1
  2
  3

Specifying a list of column names is keeps is also possible. Keep columns ‘a’ and ‘c’ of the table:

>>> t = Table([[1, 2, 3],[0.1, 0.2, 0.3],['x', 'y', 'z']],
...           names=('a', 'b', 'c'))
>>> t.keep_columns(['a', 'c'])
>>> print(t)
 a   c
--- ---
  1   x
  2   y
  3   z
property loc

Return a TableLoc object that can be used for retrieving rows by index in a given data range. Note that both loc and iloc work only with single-column indices.

property loc_indices

Return a TableLocIndices object that can be used for retrieving the row indices corresponding to given table index key value or values.

more(self, max_lines=None, max_width=None, show_name=True, show_unit=None, show_dtype=False)

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 is True.

show_unit : bool

Include a header row for unit. Default is to show a row for units only if one or more columns has a defined value for the unit.

show_dtype : bool

Include a header row for column dtypes. Default is True.

pformat(self, max_lines=None, max_width=None, show_name=True, show_unit=None, show_dtype=False, html=False, tableid=None, align=None, tableclass=None)
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.conf.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.conf.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 is True.

show_unit : bool

Include a header row for unit. Default is to show a row for units only if one or more columns has a defined value for the unit.

show_dtype : bool

Include a header row for column dtypes. Default is True.

html : bool

Format the output as an HTML table. Default is False.

tableid : str or None

An ID tag for the table; only used if html is set. Default is “table{id}”, where id is the unique integer id of the table object, id(self)

align : str or list or tuple or None

Left/right alignment of columns. Default is right (None) for all columns. Other allowed values are ‘>’, ‘<’, ‘^’, and ‘0=’ for right, left, centered, and 0-padded, respectively. A list of strings can be provided for alignment of tables with multiple columns.

tableclass : str or list of str or None

CSS classes for the table; only used if html is set. Default is None.

Returns

lines : list

Formatted table as a list of strings.

pformat_all(self, max_lines=-1, max_width=-1, show_name=True, show_unit=None, show_dtype=False, html=False, tableid=None, align=None, tableclass=None)
Return a list of lines for the formatted string representation of

the entire 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.conf.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.conf.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 is True.

show_unit : bool

Include a header row for unit. Default is to show a row for units only if one or more columns has a defined value for the unit.

show_dtype : bool

Include a header row for column dtypes. Default is True.

html : bool

Format the output as an HTML table. Default is False.

tableid : str or None

An ID tag for the table; only used if html is set. Default is “table{id}”, where id is the unique integer id of the table object, id(self)

align : str or list or tuple or None

Left/right alignment of columns. Default is right (None) for all columns. Other allowed values are ‘>’, ‘<’, ‘^’, and ‘0=’ for right, left, centered, and 0-padded, respectively. A list of strings can be provided for alignment of tables with multiple columns.

tableclass : str or list of str or None

CSS classes for the table; only used if html is set. Default is None.

Returns

lines : list

Formatted table as a list of strings.

pprint(self, max_lines=None, max_width=None, show_name=True, show_unit=None, show_dtype=False, align=None)

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.conf.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.conf.max_width.

Parameters

max_lines : int or None

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 is True.

show_unit : bool

Include a header row for unit. Default is to show a row for units only if one or more columns has a defined value for the unit.

show_dtype : bool

Include a header row for column dtypes. Default is True.

align : str or list or tuple or None

Left/right alignment of columns. Default is right (None) for all columns. Other allowed values are ‘>’, ‘<’, ‘^’, and ‘0=’ for right, left, centered, and 0-padded, respectively. A list of strings can be provided for alignment of tables with multiple columns.

pprint_all(self, max_lines=-1, max_width=-1, show_name=True, show_unit=None, show_dtype=False, align=None)

Print a formatted string representation of the entire table.

This method is the same as astropy.table.Table.pprint except that the default max_lines and max_width are both -1 so that by default the entire table is printed instead of restricting to the size of the screen terminal.

Parameters

max_lines : int or None

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 is True.

show_unit : bool

Include a header row for unit. Default is to show a row for units only if one or more columns has a defined value for the unit.

show_dtype : bool

Include a header row for column dtypes. Default is True.

align : str or list or tuple or None

Left/right alignment of columns. Default is right (None) for all columns. Other allowed values are ‘>’, ‘<’, ‘^’, and ‘0=’ for right, left, centered, and 0-padded, respectively. A list of strings can be provided for alignment of tables with multiple columns.

remove_column(self, name)

Remove a column from the table.

This can also be done with:

del table[name]
Parameters

name : str

Name of column to remove

Examples

Create a table with three columns ‘a’, ‘b’ and ‘c’:

>>> t = Table([[1, 2, 3], [0.1, 0.2, 0.3], ['x', 'y', 'z']],
...           names=('a', 'b', 'c'))
>>> print(t)
 a   b   c
--- --- ---
  1 0.1   x
  2 0.2   y
  3 0.3   z

Remove column ‘b’ from the table:

>>> t.remove_column('b')
>>> print(t)
 a   c
--- ---
  1   x
  2   y
  3   z

To remove several columns at the same time use remove_columns.

remove_columns(self, names)

Remove several columns from the table.

Parameters

names : list

A list containing the names of the columns to remove

Examples

Create a table with three columns ‘a’, ‘b’ and ‘c’:

>>> t = Table([[1, 2, 3], [0.1, 0.2, 0.3], ['x', 'y', 'z']],
...     names=('a', 'b', 'c'))
>>> print(t)
 a   b   c
--- --- ---
  1 0.1   x
  2 0.2   y
  3 0.3   z

Remove columns ‘b’ and ‘c’ from the table:

>>> t.remove_columns(['b', 'c'])
>>> print(t)
 a
---
  1
  2
  3

Specifying only a single column also works. Remove column ‘b’ from the table:

>>> t = Table([[1, 2, 3], [0.1, 0.2, 0.3], ['x', 'y', 'z']],
...     names=('a', 'b', 'c'))
>>> t.remove_columns('b')
>>> print(t)
 a   c
--- ---
  1   x
  2   y
  3   z

This gives the same as using remove_column.

remove_indices(self, colname)

Remove all indices involving the given column. If the primary index is removed, the new primary index will be the most recently added remaining index.

Parameters

colname : str

Name of column

remove_row(self, index)

Remove a row from the table.

Parameters

index : int

Index of row to remove

Examples

Create a table with three columns ‘a’, ‘b’ and ‘c’:

>>> t = Table([[1, 2, 3], [0.1, 0.2, 0.3], ['x', 'y', 'z']],
...           names=('a', 'b', 'c'))
>>> print(t)
 a   b   c
--- --- ---
  1 0.1   x
  2 0.2   y
  3 0.3   z

Remove row 1 from the table:

>>> t.remove_row(1)
>>> print(t)
 a   b   c
--- --- ---
  1 0.1   x
  3 0.3   z

To remove several rows at the same time use remove_rows.

remove_rows(self, row_specifier)

Remove rows from the table.

Parameters

row_specifier : slice, int, or array of ints

Specification for rows to remove

Examples

Create a table with three columns ‘a’, ‘b’ and ‘c’:

>>> t = Table([[1, 2, 3], [0.1, 0.2, 0.3], ['x', 'y', 'z']],
...           names=('a', 'b', 'c'))
>>> print(t)
 a   b   c
--- --- ---
  1 0.1   x
  2 0.2   y
  3 0.3   z

Remove rows 0 and 2 from the table:

>>> t.remove_rows([0, 2])
>>> print(t)
 a   b   c
--- --- ---
  2 0.2   y

Note that there are no warnings if the slice operator extends outside the data:

>>> t = Table([[1, 2, 3], [0.1, 0.2, 0.3], ['x', 'y', 'z']],
...           names=('a', 'b', 'c'))
>>> t.remove_rows(slice(10, 20, 1))
>>> print(t)
 a   b   c
--- --- ---
  1 0.1   x
  2 0.2   y
  3 0.3   z
rename_column(self, 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

TODO: this won’t work for mixins

Parameters

name : str

The current name of the column.

new_name : str

The new name for the column

Examples

Create a table with three columns ‘a’, ‘b’ and ‘c’:

>>> t = Table([[1,2],[3,4],[5,6]], names=('a','b','c'))
>>> print(t)
 a   b   c
--- --- ---
  1   3   5
  2   4   6

Renaming column ‘a’ to ‘aa’:

>>> t.rename_column('a' , 'aa')
>>> print(t)
 aa  b   c
--- --- ---
  1   3   5
  2   4   6
rename_columns(self, names, new_names)

Rename multiple columns.

Parameters

names : list, tuple

A list or tuple of existing column names.

new_names : list, tuple

A list or tuple of new column names.

Examples

Create a table with three columns ‘a’, ‘b’, ‘c’:

>>> t = Table([[1,2],[3,4],[5,6]], names=('a','b','c'))
>>> print(t)
  a   b   c
 --- --- ---
  1   3   5
  2   4   6

Renaming columns ‘a’ to ‘aa’ and ‘b’ to ‘bb’:

>>> names = ('a','b')
>>> new_names = ('aa','bb')
>>> t.rename_columns(names, new_names)
>>> print(t)
 aa  bb   c
--- --- ---
  1   3   5
  2   4   6
replace_column(self, name, col)

Replace column name with the new col object.

Parameters

name : str

Name of column to replace

col : column object (list, ndarray, Column, etc)

New column object to replace the existing column

Examples

Replace column ‘a’ with a float version of itself:

>>> t = Table([[1, 2, 3], [0.1, 0.2, 0.3]], names=('a', 'b'))
>>> float_a = t['a'].astype(float)
>>> t.replace_column('a', float_a)
reverse(self)

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

Examples

Create a table with three columns:

>>> t = Table([['Max', 'Jo', 'John'], ['Miller','Miller','Jackson'],
...         [12,15,18]], names=('firstname','name','tel'))
>>> print(t)
firstname   name  tel
--------- ------- ---
      Max  Miller  12
       Jo  Miller  15
     John Jackson  18

Reversing order:

>>> t.reverse()
>>> print(t)
firstname   name  tel
--------- ------- ---
     John Jackson  18
       Jo  Miller  15
      Max  Miller  12
show_in_browser(self, max_lines=5000, jsviewer=False, browser='default', jskwargs={'use_local_files': True}, tableid=None, table_class='display compact', css=None, show_row_index='idx')

Render the table in HTML and show it in a web browser.

Parameters

max_lines : int

Maximum number of rows to export to the table (set low by default to avoid memory issues, since the browser view requires duplicating the table in memory). A negative value of max_lines indicates no row limit.

jsviewer : bool

If True, prepends some javascript headers so that the table is rendered as a DataTables data table. This allows in-browser searching & sorting.

browser : str

Any legal browser name, e.g. 'firefox', 'chrome', 'safari' (for mac, you may need to use 'open -a "/Applications/Google Chrome.app" {}' for Chrome). If 'default', will use the system default browser.

jskwargs : dict

Passed to the astropy.table.JSViewer init. Defaults to {'use_local_files': True} which means that the JavaScript libraries will be served from local copies.

tableid : str or None

An html ID tag for the table. Default is table{id}, where id is the unique integer id of the table object, id(self).

table_class : str or None

A string with a list of HTML classes used to style the table. Default is “display compact”, and other possible values can be found in https://www.datatables.net/manual/styling/classes

css : string

A valid CSS string declaring the formatting for the table. Defaults to astropy.table.jsviewer.DEFAULT_CSS.

show_row_index : str or False

If this does not evaluate to False, a column with the given name will be added to the version of the table that gets displayed. This new column shows the index of the row in the table itself, even when the displayed table is re-sorted by another column. Note that if a column with this name already exists, this option will be ignored. Defaults to “idx”.

show_in_notebook(self, tableid=None, css=None, display_length=50, table_class='astropy-default', show_row_index='idx')

Render the table in HTML and show it in the IPython notebook.

Parameters

tableid : str or None

An html ID tag for the table. Default is table{id}-XXX, where id is the unique integer id of the table object, id(self), and XXX is a random number to avoid conflicts when printing the same table multiple times.

table_class : str or None

A string with a list of HTML classes used to style the table. The special default string (‘astropy-default’) means that the string will be retrieved from the configuration item astropy.table.default_notebook_table_class. Note that these table classes may make use of bootstrap, as this is loaded with the notebook. See this page for the list of classes.

css : string

A valid CSS string declaring the formatting for the table. Defaults to astropy.table.jsviewer.DEFAULT_CSS_NB.

display_length : int, optional

Number or rows to show. Defaults to 50.

show_row_index : str or False

If this does not evaluate to False, a column with the given name will be added to the version of the table that gets displayed. This new column shows the index of the row in the table itself, even when the displayed table is re-sorted by another column. Note that if a column with this name already exists, this option will be ignored. Defaults to “idx”.

Notes

Currently, unlike show_in_browser (with jsviewer=True), this method needs to access online javascript code repositories. This is due to modern browsers’ limitations on accessing local files. Hence, if you call this method while offline (and don’t have a cached version of jquery and jquery.dataTables), you will not get the jsviewer features.

sort(self, keys=None, reverse=False)

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. If None, use the primary index of the Table.

reverse : bool

Sort in reverse order (default=False)

Examples

Create a table with 3 columns:

>>> t = Table([['Max', 'Jo', 'John'], ['Miller', 'Miller', 'Jackson'],
...            [12, 15, 18]], names=('firstname', 'name', 'tel'))
>>> print(t)
firstname   name  tel
--------- ------- ---
      Max  Miller  12
       Jo  Miller  15
     John Jackson  18

Sorting according to standard sorting rules, first ‘name’ then ‘firstname’:

>>> t.sort(['name', 'firstname'])
>>> print(t)
firstname   name  tel
--------- ------- ---
     John Jackson  18
       Jo  Miller  15
      Max  Miller  12

Sorting according to standard sorting rules, first ‘firstname’ then ‘tel’, in reverse order:

>>> t.sort(['firstname', 'tel'], reverse=True)
>>> print(t)
firstname   name  tel
--------- ------- ---
      Max  Miller  12
     John Jackson  18
       Jo  Miller  15
to_pandas(self, index=None)

Return a pandas.DataFrame instance

The index of the created DataFrame is controlled by the index argument. For index=True or the default None, an index will be specified for the DataFrame if there is a primary key index on the Table and if it corresponds to a single column. If index=False then no DataFrame index will be specified. If index is the name of a column in the table then that will be the DataFrame index.

In additional to vanilla columns or masked columns, this supports Table mixin columns like Quantity, Time, or SkyCoord. In many cases these objects have no analog in pandas and will be converted to a “encoded” representation using only Column or MaskedColumn. The exception is Time or TimeDelta columns, which will be converted to the corresponding representation in pandas using np.datetime64 or np.timedelta64. See the example below.

Returns

dataframe : pandas.DataFrame

A pandas pandas.DataFrame instance

index : None, bool, str

Specify DataFrame index mode

Raises

ImportError

If pandas is not installed

ValueError

If the Table has multi-dimensional columns

Examples

Here we convert a table with a few mixins to a pandas.DataFrame instance.

>>> import pandas as pd
>>> from astropy.table import QTable
>>> import astropy.units as u
>>> from astropy.time import Time, TimeDelta
>>> from astropy.coordinates import SkyCoord
>>> q = [1, 2] * u.m
>>> tm = Time([1998, 2002], format='jyear')
>>> sc = SkyCoord([5, 6], [7, 8], unit='deg')
>>> dt = TimeDelta([3, 200] * u.s)
>>> t = QTable([q, tm, sc, dt], names=['q', 'tm', 'sc', 'dt'])
>>> df = t.to_pandas(index='tm')
>>> with pd.option_context('display.max_columns', 20):
...     print(df)
              q  sc.ra  sc.dec       dt
tm
1998-01-01  1.0    5.0     7.0 00:00:03
2002-01-01  2.0    6.0     8.0 00:03:20