HTable

class kanon.tables.htable.HTable(data=None, names: Optional[Union[List[str], Tuple[str, ...]]] = None, index: Optional[Union[str, List[str]]] = None, dtype: Optional[List] = None, units: Optional[List[astropy.units.Unit]] = None, *args, **kwargs)[source] [edit on github]

Bases: astropy.table.Table

HTable is a subclass of astropy.table.Table, made to model Historical Astronomy tables representing mathematical functions. Its argument column or columns are its index, while the values should be on the first column. Columns are allowed to contain all kinds of Real, especially BasedReal numbers. HTable also provides additional historical features and metadata.

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

>>> table = HTable({"args": [1,2,3], "values": [5.1,3.9,4.3]}, index="args")
>>> table
<HTable length=3>
args  values
int64 float64
----- -------
    1     5.1
    2     3.9
    3     4.3
>>> table.loc[2]
<Row index=1>
args  values
int64 float64
----- -------
    2     3.9
>>> table.loc[2]["values"]
3.9
Parameters
  • data (Optional[Data]) – Data to initialize table.

  • names (Union[List[str], Tuple[str, ...]]) – Specify column names.

  • index (Optional[Union[str, List[str]]]) – Columns considered as the indices.

  • units (Optional[List[Unit]]) – List or dict of units to apply to columns.

  • dtype (Optional[List]) – Specify column data types.

  • symmetry (Optional[List[Symmetry]]) – Specify a list of Symmetry on this table. Defaults to empty list.

  • interpolate (Optional[Interpolator]) – Specify a custom interpolation method, defaults to linear_interpolation.

  • opposite (Optional[bool]) – Defines if the table values should be of the opposite sign. Defaults to False.

Attributes Summary

interpolate

Interpolation method.

is_double

Is this table a double argument table

opposite

Defines if the table values should be of the opposite sign.

symmetry

Table symmetries.

values_column

Column representing the values

Methods Summary

apply(column, func[, new_name])

Applies a function on a column and returns the new HTable

copy([set_index, copy_data])

Return a copy of the table.

diff([n, prepend, append, new_name])

Applies np.diff on this table's column values to calculate the n-th difference.

fill(method[, bounds])

Fill masked values within bounds with the specified method.

get()

Get the value from any key based on interpolated data.

plot2d(*args, **kwargs)

Plot this HTable with the argument column as X and values column as Y.

populate(array[, method])

Populate a table with values from new keys contained in array.

set_index(index[, engine])

to_pandas([index, use_nullable_int, symmetry])

Return a pandas.DataFrame instance

Attributes Documentation

interpolate

Interpolation method.

is_double

Is this table a double argument table

opposite: bool

Defines if the table values should be of the opposite sign.

symmetry: List[kanon.tables.symmetries.Symmetry]

Table symmetries.

values_column

Column representing the values

Methods Documentation

apply(column: str, func: Callable, new_name: Optional[str] = None) kanon.tables.htable.HTable[source] [edit on github]

Applies a function on a column and returns the new HTable

Parameters
  • column – Name of the column to be modified

  • func – Function applied to the column

  • new_name – New name for the modified column, optional

Returns

HTable with modified column

Return type

HTable

copy(set_index=None, copy_data=True) kanon.tables.htable.HTable[source] [edit on github]

Return a copy of the table.

Parameters
copy_databool

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.

diff(n=1, prepend: List = [], append: List = [], new_name: Optional[str] = None) kanon.tables.htable.HTable[source] [edit on github]

Applies np.diff on this table’s column values to calculate the n-th difference. By default, it will prepend the first value.

Returns

HTable with n-th difference values.

Return type

HTable

fill(method: Union[Literal['distributed_convex', 'distributed_concave'], Callable[[pandas.core.frame.DataFrame], pandas.core.frame.DataFrame]], bounds: Optional[Tuple[Union[float, numbers.Real], Union[float, numbers.Real]]] = None) kanon.tables.htable.HTable[source] [edit on github]

Fill masked values within bounds with the specified method. To fill with a unique value, see filled.

Parameters
  • method (Literal["distributed_convex", "distributed_concave"]) – Method to use for filling masked values

  • bounds (Optional[Tuple[Real, Real]], optional) – Tuple of arguments bounds, defaults to the whole table

get(key: Union[float, numbers.Real, astropy.units.quantity.Quantity], with_unit: Literal[False]) Union[float, numbers.Real][source] [edit on github]
get(key: Union[float, numbers.Real], with_unit: Literal[True] = True) Union[float, numbers.Real, astropy.units.quantity.Quantity]
get(key: astropy.units.quantity.Quantity, with_unit: Literal[True] = True) astropy.units.quantity.Quantity

Get the value from any key based on interpolated data.

Parameters
  • key (Real) – Argument for an interpolated function

  • with_unit (bool) – Whether the result is represented as a Quantity or not. Defaults to True

Raises

IndexError – Key is out of bounds

Returns

Interpolated value

Return type

Real

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

Plot this HTable with the argument column as X and values column as Y. Uses the same arguments as matplotlib.pyplot.plot.

populate(array: List[Union[float, numbers.Real]], method: Literal['mask', 'interpolate'] = 'mask') kanon.tables.htable.HTable[source] [edit on github]

Populate a table with values from new keys contained in array.

set_index(index: Union[str, List[str]], engine=None)[source] [edit on github]
to_pandas(index=None, use_nullable_int=True, symmetry=True) pandas.core.frame.DataFrame[source] [edit on github]

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

Parameters
indexNone, bool, str

Specify DataFrame index mode

use_nullable_intbool, default=True

Convert integer MaskedColumn to pandas nullable integer type. If use_nullable_int=False or the pandas version does not support nullable integer types (version < 0.24), then the column is converted to float with NaN for missing elements and a warning is issued.

Returns
dataframepandas.DataFrame

A pandas pandas.DataFrame instance

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 0 days 00:00:03
2002-01-01  2.0    6.0     8.0 0 days 00:03:20