Column¶
-
class
astropy.table.Column(data=None, name=None, dtype=None, shape=(), length=0, description=None, unit=None, format=None, meta=None, copy=False, copy_indices=True)[source]¶ Bases:
astropy.table.column.BaseColumnDefine a data column for use in a Table object.
- Parameters
- datalist, ndarray or None
Column data values
- namestr
Column name and key for reference within Table
- dtypenumpy.dtype compatible value
Data type for column
- shapetuple or ()
Dimensions of a single row element in the column data
- lengthint or 0
Number of row elements in column data
- descriptionstr or None
Full description of column
- unitstr or None
Physical unit
- formatstr 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.- metadict-like or None
Meta-data associated with the column
Examples
A Column can be created in two different ways:
Provide a
datavalue but notshapeorlength(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
dtypeargument can be any value which is an acceptable fixed-size data-type initializer for the numpy.dtype() method. See https://numpy.org/doc/stable/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
dtypevalue is provide then the type is inferred usingnp.array(data).Provide
lengthand optionallyshape, but notdataExamples:
col = Column(name='name', length=5) col = Column(name='name', dtype=int, length=10, shape=(3,4))
The default
dtypeisnp.float64. Theshapeargument is the array shape of a single cell in the column.
Attributes Summary
The name of this column.
A view of this table column as a
Quantityobject with units given by the Column’sunitparameter.The unit associated with this column.
Methods Summary
convert_unit_to(new_unit[, equivalencies])Converts the values of the column in-place from the current unit to the given unit.
copy([order, data, copy_data])Return a copy of the current instance.
insert(obj, values[, axis])Insert values before the given indices in the column and return a new
Columnobject.more([max_lines, show_name, show_unit])Interactively browse column with a paging interface.
pformat([max_lines, show_name, show_unit, …])Return a list of formatted string representation of column values.
pprint([max_lines, show_name, show_unit, …])Print a formatted string representation of column values.
to(unit[, equivalencies])Converts this table column to a
Quantityobject with the requested units.Attributes Documentation
-
name¶ The name of this column.
-
quantity¶ A view of this table column as a
Quantityobject with units given by the Column’sunitparameter.
-
unit¶ The unit associated with this column. May be a string or a
astropy.units.UnitBaseinstance.Setting the
unitproperty does not change the values of the data. To perform a unit conversion, useconvert_unit_to.
Methods Documentation
-
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
unitproperty.- Parameters
- new_unitstr or
astropy.units.UnitBaseinstance The unit to convert to.
- equivalencieslist of equivalence pairs, optional
A list of equivalence pairs to try if the unit are not directly convertible. See Equivalencies.
- new_unitstr or
- Raises
- astropy.units.UnitsError
If units are inconsistent
-
copy(order='C', data=None, copy_data=True)¶ Return a copy of the current instance.
If
datais supplied then a view (reference) ofdatais used, andcopy_datais 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
ais Fortran contiguous, ‘C’ otherwise. ‘K’ means match the layout ofaas 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’.- dataarray, optional
If supplied then use a view of
datainstead of the instance data. This allows copying the instance attributes and meta.- copy_databool, optional
Make a copy of the internal numpy array instead of using a reference. Default is True.
- Returns
- colColumn or MaskedColumn
Copy of the current column (same type as original)
-
insert(obj, values, axis=0)[source]¶ Insert values before the given indices in the column and return a new
Columnobject.- Parameters
- objint, slice or sequence of ints
Object that defines the index or indices before which
valuesis inserted.- valuesarray_like
Value(s) to insert. If the type of
valuesis different from that of the column,valuesis converted to the matching type.valuesshould be shaped so that it can be broadcast appropriately.- axisint, optional
Axis along which to insert
values. Ifaxisis 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
valuesandmaskinserted. Note that the insertion does not occur in-place: a new column is returned.
- 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_linesint
Maximum number of lines in table output.
- show_namebool
Include a header row for column names. Default is True.
- show_unitbool
Include a header row for unit. Default is False.
-
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_linesis supplied then the height of the screen terminal is used to setmax_lines. If the terminal height cannot be determined then the default will be determined using theastropy.conf.max_linesconfiguration item. If a negative value ofmax_linesis supplied then there is no line limit applied.- Parameters
- max_linesint
Maximum lines of output (header + data rows)
- show_namebool
Include column name. Default is True.
- show_unitbool
Include a header row for unit. Default is False.
- show_dtypebool
Include column dtype. Default is False.
- htmlbool
Format the output as an HTML table. Default is False.
- Returns
- lineslist
List of lines with header and formatted column values
-
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_linesis supplied then the height of the screen terminal is used to setmax_lines. If the terminal height cannot be determined then the default will be determined using theastropy.conf.max_linesconfiguration item. If a negative value ofmax_linesis supplied then there is no line limit applied.- Parameters
- max_linesint
Maximum number of values in output
- show_namebool
Include column name. Default is True.
- show_unitbool
Include a header row for unit. Default is False.
- show_dtypebool
Include column dtype. Default is True.
-
to(unit, equivalencies=[], **kwargs)¶ Converts this table column to a
Quantityobject with the requested units.- Parameters
- unit
Unitor str The unit to convert to (i.e., a valid argument to the
astropy.units.Quantity.to()method).- equivalencieslist of equivalence pairs, optional
Equivalencies to use for this conversion. See
astropy.units.Quantity.to()for more details.
- unit
- Returns
- quantity
Quantity A quantity object with the contents of this column in the units
unit.
- quantity