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, 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 unit, format, and description.
Parameters: | data : numpy ndarray, dict, list, Table, or table-like object, optional
masked : bool, optional
names : list, optional
dtype : list, optional
meta : dict, optional
copy : bool, optional
rows : numpy ndarray, list of lists, optional
copy_indices : bool, optional
**kwargs : dict, optional
.. note::
|
---|
Methods Summary
add_column (col[, index, name, rename_duplicate]) |
Add a new Column object col to the table. |
add_columns (cols[, indexes, names, copy, …]) |
Add a list of new Column objects cols to the table. |
add_index (colnames[, engine, unique]) |
Insert a new index among one or more columns. |
add_row ([vals, mask]) |
Add a new row to the end of the table. |
argsort ([keys, kind]) |
Return the indices which would sort the table according to one or more key columns. |
as_array ([keep_byteorder]) |
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 ([python3_only]) |
Convert bytestring columns (dtype.kind=’S’) to unicode (dtype.kind=’U’) assuming ASCII encoding. |
convert_unicode_to_bytestring ([python3_only]) |
Convert ASCII-only unicode columns (dtype.kind=’U’) to bytestring (dtype.kind=’S’). |
copy ([copy_data]) |
Return a copy of the table. |
field (item) |
Return column[item] for recarray compatibility. |
filled ([fill_value]) |
Return a copy of self, with masked values filled. |
from_pandas (dataframe) |
Create a Table from a pandas.DataFrame instance |
group_by (keys) |
Group this table by the specified keys |
index_column (name) |
Return the positional index of column name . |
index_mode (mode) |
Return a context manager for an indexing mode. |
insert_row (index[, vals, mask]) |
Add a new row before the given index position in the table. |
itercols () |
Iterate over the columns of this table. |
keep_columns (names) |
Keep only the columns specified (remove the others). |
keys () |
|
more ([max_lines, max_width, show_name, …]) |
Interactively browse table with a paging interface. |
pformat ([max_lines, max_width, show_name, …]) |
Return a list of lines for the formatted string representation of the table. |
pprint ([max_lines, max_width, show_name, …]) |
Print a formatted string representation of the table. |
read (*args, **kwargs) |
Read and parse a data table and return as a Table. |
remove_column (name) |
Remove a column from the table. |
remove_columns (names) |
Remove several columns from the table. |
remove_indices (colname) |
Remove all indices involving the given column. |
remove_row (index) |
Remove a row from the table. |
remove_rows (row_specifier) |
Remove rows from the table. |
rename_column (name, new_name) |
Rename a column. |
replace_column (name, col) |
Replace column name with the new col object. |
reverse () |
Reverse the row order of table rows. |
show_in_browser ([max_lines, jsviewer, …]) |
Render the table in HTML and show it in a web browser. |
show_in_notebook ([tableid, css, …]) |
Render the table in HTML and show it in the IPython notebook. |
sort ([keys]) |
Sort the table according to one or more keys. |
to_pandas () |
Return a pandas.DataFrame instance |
write (*args, **kwargs) |
Write this Table object out in the specified format. |
Methods Documentation
add_column
(col, index=None, name=None, rename_duplicate=False)¶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
index : int or
name : str
rename_duplicate : bool
|
---|
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
(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
indexes : list of ints or
names : list of str
copy : bool
rename_duplicate : bool
|
---|
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
(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
engine : type or None
unique : bool
|
---|
add_row
(vals=None, mask=None)¶Add a new row to the end of the table.
The vals
argument can be:
None
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
mask : tuple, list, dict or
|
---|
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
(keys=None, kind=None)¶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
kind : {‘quicksort’, ‘mergesort’, ‘heapsort’}, optional
|
---|---|
Returns: | index_array : ndarray, int
|
as_array
(keep_byteorder=False)¶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
|
---|---|
Returns: | table_array : np.ndarray (unmasked) or np.ma.MaskedArray (masked)
|
convert_bytestring_to_unicode
(python3_only=False)¶Convert bytestring columns (dtype.kind=’S’) to unicode (dtype.kind=’U’) assuming ASCII 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 Python 3 scripts to manipulate string arrays with natural syntax.
The python3_only
parameter is provided as a convenience so that code can
be written in a Python 2 / 3 compatible way:
>>> t = Table.read('my_data.fits')
>>> t.convert_bytestring_to_unicode(python3_only=True)
Parameters: | python3_only : bool
|
---|
convert_unicode_to_bytestring
(python3_only=False)¶Convert ASCII-only unicode columns (dtype.kind=’U’) to bytestring (dtype.kind=’S’).
When exporting a unicode string array to a file in Python 3, it may be desirable to encode unicode columns as bytestrings. This routine takes advantage of numpy automated conversion which works for strings that are pure ASCII.
The python3_only
parameter is provided as a convenience so that code can
be written in a Python 2 / 3 compatible way:
>>> t.convert_unicode_to_bytestring(python3_only=True)
>>> t.write('my_data.fits')
Parameters: | python3_only : bool
|
---|
copy
(copy_data=True)¶Return a copy of the table.
Parameters: | copy_data : bool
.. note::
|
---|
field
(item)¶Return column[item] for recarray compatibility.
filled
(fill_value=None)¶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.
Parameters: | fill_value : str
|
---|---|
Returns: | filled_table : Table
|
from_pandas
(dataframe)¶Create a Table
from a pandas.DataFrame
instance
Parameters: | dataframe :
|
---|---|
Returns: | table :
|
group_by
(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
|
---|---|
Returns: | out :
|
index_column
(name)¶Return the positional index of column name
.
Parameters: | name : str
|
---|---|
Returns: | index : int
|
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
(mode)¶Return a context manager for an indexing mode.
Parameters: | mode : str
|
---|
insert_row
(index, vals=None, mask=None)¶Add a new row before the given index
position in the table.
The vals
argument can be:
None
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
mask : tuple, list, dict or
|
---|
itercols
()¶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
(names)¶Keep only the columns specified (remove the others).
Parameters: | names : list
|
---|
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
()¶more
(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
max_width : int or
show_name : bool
show_unit : bool
show_dtype : bool
|
---|
pformat
(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
max_width : int or
show_name : bool
show_unit : bool
show_dtype : bool
html : bool
tableid : str or
align : str or list or tuple or
tableclass : str or list of str or
|
---|---|
Returns: | lines : list
|
pprint
(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
max_width : int or
show_name : bool
show_unit : bool
show_dtype : bool
align : str or list or tuple or
|
---|
read
(*args, **kwargs)¶Read and parse a data table and return as a Table.
This function provides the Table interface to the astropy unified I/O layer. This allows easily reading a file in many supported data formats using syntax such as:
>>> from astropy.table import Table
>>> dat = Table.read('table.dat', format='ascii')
>>> events = Table.read('events.fits', format='fits')
The arguments and keywords (other than format
) provided to this function are
passed through to the underlying data reader (e.g. read
).
The available built-in formats are:
Format | Read | Write | Auto-identify | Deprecated |
---|---|---|---|---|
ascii | Yes | Yes | No | |
ascii.aastex | Yes | Yes | No | |
ascii.basic | Yes | Yes | No | |
ascii.cds | Yes | No | No | |
ascii.commented_header | Yes | Yes | No | |
ascii.csv | Yes | Yes | No | |
ascii.cwb | Yes | Yes | No | |
ascii.daophot | Yes | No | No | |
ascii.ecsv | Yes | Yes | No | |
ascii.fast_basic | Yes | Yes | No | |
ascii.fast_commented_header | Yes | Yes | No | |
ascii.fast_csv | Yes | Yes | No | |
ascii.fast_no_header | Yes | Yes | No | |
ascii.fast_rdb | Yes | Yes | No | |
ascii.fast_tab | Yes | Yes | No | |
ascii.fixed_width | Yes | Yes | No | |
ascii.fixed_width_no_header | Yes | Yes | No | |
ascii.fixed_width_two_line | Yes | Yes | No | |
ascii.html | Yes | Yes | Yes | |
ascii.ipac | Yes | Yes | No | |
ascii.latex | Yes | Yes | Yes | |
ascii.no_header | Yes | Yes | No | |
ascii.omega | Yes | Yes | No | |
ascii.rdb | Yes | Yes | Yes | |
ascii.rst | Yes | Yes | No | |
ascii.sextractor | Yes | No | No | |
ascii.tab | Yes | Yes | No | |
fits | Yes | Yes | Yes | |
gwf | Yes | No | No | |
hdf5 | Yes | Yes | Yes | |
ligolw | Yes | Yes | Yes | |
ligolw.coinc_definer | Yes | Yes | No | |
ligolw.coinc_event | Yes | Yes | No | |
ligolw.coinc_event_map | Yes | Yes | No | |
ligolw.coinc_inspiral | Yes | Yes | No | |
ligolw.coinc_ringdown | Yes | Yes | No | |
ligolw.dq_list | Yes | Yes | No | |
ligolw.experiment | Yes | Yes | No | |
ligolw.experiment_map | Yes | Yes | No | |
ligolw.experiment_summary | Yes | Yes | No | |
ligolw.external_trigger | Yes | Yes | No | |
ligolw.filter | Yes | Yes | No | |
ligolw.gds_trigger | Yes | Yes | No | |
ligolw.lfn | Yes | Yes | No | |
ligolw.ligolw_mon | Yes | Yes | No | |
ligolw.multi_burst | Yes | Yes | No | |
ligolw.multi_inspiral | Yes | Yes | No | |
ligolw.process | Yes | Yes | No | |
ligolw.process_params | Yes | Yes | No | |
ligolw.search_summary | Yes | Yes | No | |
ligolw.search_summvars | Yes | Yes | No | |
ligolw.segment | Yes | Yes | No | |
ligolw.segment_definer | Yes | Yes | No | |
ligolw.segment_summary | Yes | Yes | No | |
ligolw.sim_burst | Yes | Yes | No | |
ligolw.sim_inspiral | Yes | Yes | No | |
ligolw.sim_inst_params | Yes | Yes | No | |
ligolw.sim_ringdown | Yes | Yes | No | |
ligolw.sngl_burst | Yes | Yes | No | |
ligolw.sngl_inspiral | Yes | Yes | No | |
ligolw.sngl_ringdown | Yes | Yes | No | |
ligolw.stochastic | Yes | Yes | No | |
ligolw.stochsumm | Yes | Yes | No | |
ligolw.summ_mime | Yes | Yes | No | |
ligolw.summ_value | Yes | Yes | No | |
ligolw.time_slide | Yes | Yes | No | |
ligolw.time_slide_segment_map | Yes | Yes | No | |
ligolw.veto_definer | Yes | Yes | No | |
root | Yes | Yes | Yes | |
votable | Yes | Yes | Yes | |
aastex | Yes | Yes | No | Yes |
cds | Yes | No | No | Yes |
csv | Yes | Yes | Yes | Yes |
daophot | Yes | No | No | Yes |
html | Yes | Yes | No | Yes |
ipac | Yes | Yes | No | Yes |
latex | Yes | Yes | No | Yes |
rdb | Yes | Yes | No | Yes |
Deprecated format names like aastex
will be removed in a future version. Use the full
name (e.g. ascii.aastex
) instead.
remove_column
(name)¶Remove a column from the table.
This can also be done with:
del table[name]
Parameters: | name : str
|
---|
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
(names)¶Remove several columns from the table.
Parameters: | names : list
|
---|
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
(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
|
---|
remove_row
(index)¶Remove a row from the table.
Parameters: | index : int
|
---|
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
(row_specifier)¶Remove rows from the table.
Parameters: | row_specifier : slice, int, or array of ints
|
---|
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
(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
new_name : str
|
---|
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
replace_column
(name, col)¶Replace column name
with the new col
object.
Parameters: | name : str
col : column object (list, ndarray, Column, etc)
|
---|
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
()¶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
(max_lines=5000, jsviewer=False, browser=u'default', jskwargs={u'use_local_files': True}, tableid=None, table_class=u'display compact', css=None, show_row_index=u'idx')¶Render the table in HTML and show it in a web browser.
Parameters: | max_lines : int
jsviewer : bool
browser : str
jskwargs : dict
tableid : str or
table_class : str or
css : string
show_row_index : str or False
|
---|
show_in_notebook
(tableid=None, css=None, display_length=50, table_class=u'astropy-default', show_row_index=u'idx')¶Render the table in HTML and show it in the IPython notebook.
Parameters: | tableid : str or
table_class : str or
css : string
display_length : int, optional
show_row_index : str or False
|
---|
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
(keys=None)¶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
|
---|
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
to_pandas
()¶Return a pandas.DataFrame
instance
Returns: | dataframe :
|
---|---|
Raises: | ImportError
ValueError
|
write
(*args, **kwargs)¶Write this Table object out in the specified format.
This function provides the Table interface to the astropy unified I/O layer. This allows easily writing a file in many supported data formats using syntax such as:
>>> from astropy.table import Table
>>> dat = Table([[1, 2], [3, 4]], names=('a', 'b'))
>>> dat.write('table.dat', format='ascii')
The arguments and keywords (other than format
) provided to this function are
passed through to the underlying data reader (e.g. write
).
The available built-in formats are:
Format | Read | Write | Auto-identify | Deprecated |
---|---|---|---|---|
ascii | Yes | Yes | No | |
ascii.aastex | Yes | Yes | No | |
ascii.basic | Yes | Yes | No | |
ascii.commented_header | Yes | Yes | No | |
ascii.csv | Yes | Yes | No | |
ascii.cwb | Yes | Yes | No | |
ascii.ecsv | Yes | Yes | No | |
ascii.fast_basic | Yes | Yes | No | |
ascii.fast_commented_header | Yes | Yes | No | |
ascii.fast_csv | Yes | Yes | No | |
ascii.fast_no_header | Yes | Yes | No | |
ascii.fast_rdb | Yes | Yes | No | |
ascii.fast_tab | Yes | Yes | No | |
ascii.fixed_width | Yes | Yes | No | |
ascii.fixed_width_no_header | Yes | Yes | No | |
ascii.fixed_width_two_line | Yes | Yes | No | |
ascii.html | Yes | Yes | Yes | |
ascii.ipac | Yes | Yes | No | |
ascii.latex | Yes | Yes | Yes | |
ascii.no_header | Yes | Yes | No | |
ascii.omega | Yes | Yes | No | |
ascii.rdb | Yes | Yes | Yes | |
ascii.rst | Yes | Yes | No | |
ascii.tab | Yes | Yes | No | |
fits | Yes | Yes | Yes | |
gwf | Yes | Yes | No | |
hdf5 | Yes | Yes | Yes | |
jsviewer | No | Yes | No | |
ligolw | Yes | Yes | Yes | |
ligolw.coinc_definer | Yes | Yes | No | |
ligolw.coinc_event | Yes | Yes | No | |
ligolw.coinc_event_map | Yes | Yes | No | |
ligolw.coinc_inspiral | Yes | Yes | No | |
ligolw.coinc_ringdown | Yes | Yes | No | |
ligolw.dq_list | Yes | Yes | No | |
ligolw.experiment | Yes | Yes | No | |
ligolw.experiment_map | Yes | Yes | No | |
ligolw.experiment_summary | Yes | Yes | No | |
ligolw.external_trigger | Yes | Yes | No | |
ligolw.filter | Yes | Yes | No | |
ligolw.gds_trigger | Yes | Yes | No | |
ligolw.lfn | Yes | Yes | No | |
ligolw.ligolw_mon | Yes | Yes | No | |
ligolw.multi_burst | Yes | Yes | No | |
ligolw.multi_inspiral | Yes | Yes | No | |
ligolw.process | Yes | Yes | No | |
ligolw.process_params | Yes | Yes | No | |
ligolw.search_summary | Yes | Yes | No | |
ligolw.search_summvars | Yes | Yes | No | |
ligolw.segment | Yes | Yes | No | |
ligolw.segment_definer | Yes | Yes | No | |
ligolw.segment_summary | Yes | Yes | No | |
ligolw.sim_burst | Yes | Yes | No | |
ligolw.sim_inspiral | Yes | Yes | No | |
ligolw.sim_inst_params | Yes | Yes | No | |
ligolw.sim_ringdown | Yes | Yes | No | |
ligolw.sngl_burst | Yes | Yes | No | |
ligolw.sngl_inspiral | Yes | Yes | No | |
ligolw.sngl_ringdown | Yes | Yes | No | |
ligolw.stochastic | Yes | Yes | No | |
ligolw.stochsumm | Yes | Yes | No | |
ligolw.summ_mime | Yes | Yes | No | |
ligolw.summ_value | Yes | Yes | No | |
ligolw.time_slide | Yes | Yes | No | |
ligolw.time_slide_segment_map | Yes | Yes | No | |
ligolw.veto_definer | Yes | Yes | No | |
root | Yes | Yes | Yes | |
votable | Yes | Yes | Yes | |
aastex | Yes | Yes | No | Yes |
csv | Yes | Yes | Yes | Yes |
html | Yes | Yes | No | Yes |
ipac | Yes | Yes | No | Yes |
latex | Yes | Yes | No | Yes |
rdb | Yes | Yes | No | Yes |
Deprecated format names like aastex
will be removed in a future version. Use the full
name (e.g. ascii.aastex
) instead.
Column
¶Bases: astropy.table.column.BaseColumn
Define a data column for use in a Table object.
Parameters: | data : list, ndarray or None
name : str
dtype : numpy.dtype compatible value
shape : tuple or ()
length : int or 0
description : str or None
unit : str or None
format : str or None or function or callable
meta : dict-like or None
|
---|
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
http://docs.scipy.org/doc/numpy/reference/arrays.dtypes.html.
Examples include:
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
(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
equivalencies : list of equivalence pairs, optional
|
---|---|
Raises: | astropy.units.UnitsError
|
copy
(order=u'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
data : array, optional
copy_data : bool, optional
|
---|---|
Returns: | col : Column or MaskedColumn
|
insert
(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
values : array_like
axis : int, optional
|
---|---|
Returns: | out :
|
more
(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
show_name : bool
show_unit : bool
|
---|
name
¶The name of this column.
pformat
(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
show_name : bool
show_unit : bool
show_dtype : bool
html : bool
|
---|---|
Returns: | lines : list
|
pprint
(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
show_name : bool
show_unit : bool
show_dtype : bool
|
---|
quantity
¶A view of this table column as a Quantity
object with
units given by the Column’s unit
parameter.
to
(unit, equivalencies=[], **kwargs)¶Converts this table column to a Quantity
object with
the requested units.
Parameters: | unit :
equivalencies : list of equivalence pairs, optional
|
---|---|
Returns: | quantity :
|
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
.
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
name : str
mask : list, ndarray or None
fill_value : float, int, str or None
dtype : numpy.dtype compatible value
shape : tuple or ()
length : int or 0
description : str or None
unit : str or None
format : str or None or function or callable
meta : dict-like or None
|
---|
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
http://docs.scipy.org/doc/numpy/reference/arrays.dtypes.html.
Examples include:
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
(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
equivalencies : list of equivalence pairs, optional
|
---|---|
Raises: | astropy.units.UnitsError
|
copy
(order=u'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
data : array, optional
copy_data : bool, optional
|
---|---|
Returns: | col : Column or MaskedColumn
|
filled
(fill_value=None)¶Return a copy of self, with masked values filled with a given value.
Parameters: | fill_value : scalar; optional |
---|---|
Returns: | filled_column : Column
|
insert
(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
values : array_like
mask : boolean array_like
axis : int, optional
|
---|---|
Returns: | out :
|
more
(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
show_name : bool
show_unit : bool
|
---|
name
¶The name of this column.
pformat
(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
show_name : bool
show_unit : bool
show_dtype : bool
html : bool
|
---|---|
Returns: | lines : list
|
pprint
(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
show_name : bool
show_unit : bool
show_dtype : bool
|
---|
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
()¶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)
|
---|
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
|
---|
isinstance
(cls)¶Return a list of columns which are instances of the specified classes.
Parameters: | cls : class or tuple of classes
|
---|---|
Returns: | col_list : list of Columns
|
not_isinstance
(cls)¶Return a list of columns which are not instances of the specified classes.
Parameters: | cls : class or tuple of classes
|
---|---|
Returns: | col_list : list of Columns
|
add_column
(col, index=None, name=None, rename_duplicate=False)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
index : int or
name : str
rename_duplicate : bool
|
---|
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
(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
indexes : list of ints or
names : list of str
copy : bool
rename_duplicate : bool
|
---|
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
(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
engine : type or None
unique : bool
|
---|
add_row
(vals=None, mask=None)Add a new row to the end of the table.
The vals
argument can be:
None
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
mask : tuple, list, dict or
|
---|
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
(keys=None, kind=None)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
kind : {‘quicksort’, ‘mergesort’, ‘heapsort’}, optional
|
---|---|
Returns: | index_array : ndarray, int
|
as_array
(keep_byteorder=False)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
|
---|---|
Returns: | table_array : np.ndarray (unmasked) or np.ma.MaskedArray (masked)
|
convert_bytestring_to_unicode
(python3_only=False)Convert bytestring columns (dtype.kind=’S’) to unicode (dtype.kind=’U’) assuming ASCII 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 Python 3 scripts to manipulate string arrays with natural syntax.
The python3_only
parameter is provided as a convenience so that code can
be written in a Python 2 / 3 compatible way:
>>> t = Table.read('my_data.fits')
>>> t.convert_bytestring_to_unicode(python3_only=True)
Parameters: | python3_only : bool
|
---|
convert_unicode_to_bytestring
(python3_only=False)Convert ASCII-only unicode columns (dtype.kind=’U’) to bytestring (dtype.kind=’S’).
When exporting a unicode string array to a file in Python 3, it may be desirable to encode unicode columns as bytestrings. This routine takes advantage of numpy automated conversion which works for strings that are pure ASCII.
The python3_only
parameter is provided as a convenience so that code can
be written in a Python 2 / 3 compatible way:
>>> t.convert_unicode_to_bytestring(python3_only=True)
>>> t.write('my_data.fits')
Parameters: | python3_only : bool
|
---|
copy
(copy_data=True)Return a copy of the table.
Parameters: | copy_data : bool
.. note::
|
---|
field
(item)Return column[item] for recarray compatibility.
filled
(fill_value=None)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.
Parameters: | fill_value : str
|
---|---|
Returns: | filled_table : Table
|
from_pandas
(dataframe)Create a Table
from a pandas.DataFrame
instance
Parameters: | dataframe :
|
---|---|
Returns: | table :
|
group_by
(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
|
---|---|
Returns: | out :
|
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.
index_column
(name)Return the positional index of column name
.
Parameters: | name : str
|
---|---|
Returns: | index : int
|
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
(mode)Return a context manager for an indexing mode.
Parameters: | mode : str
|
---|
indices
¶Return the indices associated with columns of the table as a TableIndices object.
insert_row
(index, vals=None, mask=None)Add a new row before the given index
position in the table.
The vals
argument can be:
None
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
mask : tuple, list, dict or
|
---|
itercols
()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
(names)Keep only the columns specified (remove the others).
Parameters: | names : list
|
---|
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
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.
more
(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
max_width : int or
show_name : bool
show_unit : bool
show_dtype : bool
|
---|
pformat
(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
max_width : int or
show_name : bool
show_unit : bool
show_dtype : bool
html : bool
tableid : str or
align : str or list or tuple or
tableclass : str or list of str or
|
---|---|
Returns: | lines : list
|
pprint
(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
max_width : int or
show_name : bool
show_unit : bool
show_dtype : bool
align : str or list or tuple or
|
---|
read
(*args, **kwargs)Read and parse a data table and return as a Table.
This function provides the Table interface to the astropy unified I/O layer. This allows easily reading a file in many supported data formats using syntax such as:
>>> from astropy.table import Table
>>> dat = Table.read('table.dat', format='ascii')
>>> events = Table.read('events.fits', format='fits')
The arguments and keywords (other than format
) provided to this function are
passed through to the underlying data reader (e.g. read
).
The available built-in formats are:
Format | Read | Write | Auto-identify | Deprecated |
---|---|---|---|---|
ascii | Yes | Yes | No | |
ascii.aastex | Yes | Yes | No | |
ascii.basic | Yes | Yes | No | |
ascii.cds | Yes | No | No | |
ascii.commented_header | Yes | Yes | No | |
ascii.csv | Yes | Yes | No | |
ascii.cwb | Yes | Yes | No | |
ascii.daophot | Yes | No | No | |
ascii.ecsv | Yes | Yes | No | |
ascii.fast_basic | Yes | Yes | No | |
ascii.fast_commented_header | Yes | Yes | No | |
ascii.fast_csv | Yes | Yes | No | |
ascii.fast_no_header | Yes | Yes | No | |
ascii.fast_rdb | Yes | Yes | No | |
ascii.fast_tab | Yes | Yes | No | |
ascii.fixed_width | Yes | Yes | No | |
ascii.fixed_width_no_header | Yes | Yes | No | |
ascii.fixed_width_two_line | Yes | Yes | No | |
ascii.html | Yes | Yes | Yes | |
ascii.ipac | Yes | Yes | No | |
ascii.latex | Yes | Yes | Yes | |
ascii.no_header | Yes | Yes | No | |
ascii.omega | Yes | Yes | No | |
ascii.rdb | Yes | Yes | Yes | |
ascii.rst | Yes | Yes | No | |
ascii.sextractor | Yes | No | No | |
ascii.tab | Yes | Yes | No | |
fits | Yes | Yes | Yes | |
gwf | Yes | No | No | |
hdf5 | Yes | Yes | Yes | |
ligolw | Yes | Yes | Yes | |
ligolw.coinc_definer | Yes | Yes | No | |
ligolw.coinc_event | Yes | Yes | No | |
ligolw.coinc_event_map | Yes | Yes | No | |
ligolw.coinc_inspiral | Yes | Yes | No | |
ligolw.coinc_ringdown | Yes | Yes | No | |
ligolw.dq_list | Yes | Yes | No | |
ligolw.experiment | Yes | Yes | No | |
ligolw.experiment_map | Yes | Yes | No | |
ligolw.experiment_summary | Yes | Yes | No | |
ligolw.external_trigger | Yes | Yes | No | |
ligolw.filter | Yes | Yes | No | |
ligolw.gds_trigger | Yes | Yes | No | |
ligolw.lfn | Yes | Yes | No | |
ligolw.ligolw_mon | Yes | Yes | No | |
ligolw.multi_burst | Yes | Yes | No | |
ligolw.multi_inspiral | Yes | Yes | No | |
ligolw.process | Yes | Yes | No | |
ligolw.process_params | Yes | Yes | No | |
ligolw.search_summary | Yes | Yes | No | |
ligolw.search_summvars | Yes | Yes | No | |
ligolw.segment | Yes | Yes | No | |
ligolw.segment_definer | Yes | Yes | No | |
ligolw.segment_summary | Yes | Yes | No | |
ligolw.sim_burst | Yes | Yes | No | |
ligolw.sim_inspiral | Yes | Yes | No | |
ligolw.sim_inst_params | Yes | Yes | No | |
ligolw.sim_ringdown | Yes | Yes | No | |
ligolw.sngl_burst | Yes | Yes | No | |
ligolw.sngl_inspiral | Yes | Yes | No | |
ligolw.sngl_ringdown | Yes | Yes | No | |
ligolw.stochastic | Yes | Yes | No | |
ligolw.stochsumm | Yes | Yes | No | |
ligolw.summ_mime | Yes | Yes | No | |
ligolw.summ_value | Yes | Yes | No | |
ligolw.time_slide | Yes | Yes | No | |
ligolw.time_slide_segment_map | Yes | Yes | No | |
ligolw.veto_definer | Yes | Yes | No | |
root | Yes | Yes | Yes | |
votable | Yes | Yes | Yes | |
aastex | Yes | Yes | No | Yes |
cds | Yes | No | No | Yes |
csv | Yes | Yes | Yes | Yes |
daophot | Yes | No | No | Yes |
html | Yes | Yes | No | Yes |
ipac | Yes | Yes | No | Yes |
latex | Yes | Yes | No | Yes |
rdb | Yes | Yes | No | Yes |
Deprecated format names like aastex
will be removed in a future version. Use the full
name (e.g. ascii.aastex
) instead.
remove_column
(name)Remove a column from the table.
This can also be done with:
del table[name]
Parameters: | name : str
|
---|
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
(names)Remove several columns from the table.
Parameters: | names : list
|
---|
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
(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
|
---|
remove_row
(index)Remove a row from the table.
Parameters: | index : int
|
---|
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
(row_specifier)Remove rows from the table.
Parameters: | row_specifier : slice, int, or array of ints
|
---|
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
(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
new_name : str
|
---|
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
replace_column
(name, col)Replace column name
with the new col
object.
Parameters: | name : str
col : column object (list, ndarray, Column, etc)
|
---|
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
()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
(max_lines=5000, jsviewer=False, browser=u'default', jskwargs={u'use_local_files': True}, tableid=None, table_class=u'display compact', css=None, show_row_index=u'idx')Render the table in HTML and show it in a web browser.
Parameters: | max_lines : int
jsviewer : bool
browser : str
jskwargs : dict
tableid : str or
table_class : str or
css : string
show_row_index : str or False
|
---|
show_in_notebook
(tableid=None, css=None, display_length=50, table_class=u'astropy-default', show_row_index=u'idx')Render the table in HTML and show it in the IPython notebook.
Parameters: | tableid : str or
table_class : str or
css : string
display_length : int, optional
show_row_index : str or False
|
---|
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
(keys=None)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
|
---|
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
to_pandas
()Return a pandas.DataFrame
instance
Returns: | dataframe :
|
---|---|
Raises: | ImportError
ValueError
|
write
(*args, **kwargs)Write this Table object out in the specified format.
This function provides the Table interface to the astropy unified I/O layer. This allows easily writing a file in many supported data formats using syntax such as:
>>> from astropy.table import Table
>>> dat = Table([[1, 2], [3, 4]], names=('a', 'b'))
>>> dat.write('table.dat', format='ascii')
The arguments and keywords (other than format
) provided to this function are
passed through to the underlying data reader (e.g. write
).
The available built-in formats are:
Format | Read | Write | Auto-identify | Deprecated |
---|---|---|---|---|
ascii | Yes | Yes | No | |
ascii.aastex | Yes | Yes | No | |
ascii.basic | Yes | Yes | No | |
ascii.commented_header | Yes | Yes | No | |
ascii.csv | Yes | Yes | No | |
ascii.cwb | Yes | Yes | No | |
ascii.ecsv | Yes | Yes | No | |
ascii.fast_basic | Yes | Yes | No | |
ascii.fast_commented_header | Yes | Yes | No | |
ascii.fast_csv | Yes | Yes | No | |
ascii.fast_no_header | Yes | Yes | No | |
ascii.fast_rdb | Yes | Yes | No | |
ascii.fast_tab | Yes | Yes | No | |
ascii.fixed_width | Yes | Yes | No | |
ascii.fixed_width_no_header | Yes | Yes | No | |
ascii.fixed_width_two_line | Yes | Yes | No | |
ascii.html | Yes | Yes | Yes | |
ascii.ipac | Yes | Yes | No | |
ascii.latex | Yes | Yes | Yes | |
ascii.no_header | Yes | Yes | No | |
ascii.omega | Yes | Yes | No | |
ascii.rdb | Yes | Yes | Yes | |
ascii.rst | Yes | Yes | No | |
ascii.tab | Yes | Yes | No | |
fits | Yes | Yes | Yes | |
gwf | Yes | Yes | No | |
hdf5 | Yes | Yes | Yes | |
jsviewer | No | Yes | No | |
ligolw | Yes | Yes | Yes | |
ligolw.coinc_definer | Yes | Yes | No | |
ligolw.coinc_event | Yes | Yes | No | |
ligolw.coinc_event_map | Yes | Yes | No | |
ligolw.coinc_inspiral | Yes | Yes | No | |
ligolw.coinc_ringdown | Yes | Yes | No | |
ligolw.dq_list | Yes | Yes | No | |
ligolw.experiment | Yes | Yes | No | |
ligolw.experiment_map | Yes | Yes | No | |
ligolw.experiment_summary | Yes | Yes | No | |
ligolw.external_trigger | Yes | Yes | No | |
ligolw.filter | Yes | Yes | No | |
ligolw.gds_trigger | Yes | Yes | No | |
ligolw.lfn | Yes | Yes | No | |
ligolw.ligolw_mon | Yes | Yes | No | |
ligolw.multi_burst | Yes | Yes | No | |
ligolw.multi_inspiral | Yes | Yes | No | |
ligolw.process | Yes | Yes | No | |
ligolw.process_params | Yes | Yes | No | |
ligolw.search_summary | Yes | Yes | No | |
ligolw.search_summvars | Yes | Yes | No | |
ligolw.segment | Yes | Yes | No | |
ligolw.segment_definer | Yes | Yes | No | |
ligolw.segment_summary | Yes | Yes | No | |
ligolw.sim_burst | Yes | Yes | No | |
ligolw.sim_inspiral | Yes | Yes | No | |
ligolw.sim_inst_params | Yes | Yes | No | |
ligolw.sim_ringdown | Yes | Yes | No | |
ligolw.sngl_burst | Yes | Yes | No | |
ligolw.sngl_inspiral | Yes | Yes | No | |
ligolw.sngl_ringdown | Yes | Yes | No | |
ligolw.stochastic | Yes | Yes | No | |
ligolw.stochsumm | Yes | Yes | No | |
ligolw.summ_mime | Yes | Yes | No | |
ligolw.summ_value | Yes | Yes | No | |
ligolw.time_slide | Yes | Yes | No | |
ligolw.time_slide_segment_map | Yes | Yes | No | |
ligolw.veto_definer | Yes | Yes | No | |
root | Yes | Yes | Yes | |
votable | Yes | Yes | Yes | |
aastex | Yes | Yes | No | Yes |
csv | Yes | Yes | Yes | Yes |
html | Yes | Yes | No | Yes |
ipac | Yes | Yes | No | Yes |
latex | Yes | Yes | No | Yes |
rdb | Yes | Yes | No | Yes |
Deprecated format names like aastex
will be removed in a future version. Use the full
name (e.g. ascii.aastex
) instead.