HTable

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

Bases: 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

model

Model the table follows

opposite

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

symmetry

Table symmetries.

table_type

Table type of the table

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.

displace(column, increment)

Helper function to increment all values of column by increment

fill(method[, bounds])

Fill masked values within bounds with the specified method.

freeze()

Freezes the tables values for to_pandas and get results

from_model(model, arguments, parameters[, ...])

Build a HTable from a model, its arguments and parameters.

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

shift(column, value)

Helper function to shift a column by value

to_pandas([index, use_nullable_int, symmetry])

Return a pandas.DataFrame instance

unfreeze()

Unfreezes the tables values for to_pandas and get results

Attributes Documentation

interpolate

Interpolation method.

is_double

Is this table a double argument table

model: ModelCallable | None

Model the table follows

opposite: bool

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

symmetry: List[Symmetry]

Table symmetries.

table_type: TableType | None

Table type of the table

values_column

Column representing the values

Methods Documentation

apply(column: str, func: Callable, new_name: str | None = None) 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) 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: str | None = None) 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

displace(column: str, increment: float | Real) HTable[source] [edit on github]

Helper function to increment all values of column by increment

Parameters:
  • column (str) – Name of the column to displace

  • increment (Real) – How much to increment

Returns:

New displaced table

Return type:

HTable

fill(method: Literal['distributed_convex', 'distributed_concave'] | Callable[[DataFrame], DataFrame], bounds: Tuple[float | Real, float | Real] | None = None) 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

freeze()[source] [edit on github]

Freezes the tables values for to_pandas and get results

classmethod from_model(model: ModelCallable, arguments: List[float | Real], parameters: Tuple[float | Real, ...], arguments2: List[float | Real] | None = None, arg1_name='Index', arg2_name='Index', entries_name='Entries', units: List[Unit] | None = None) HTable[source] [edit on github]

Build a HTable from a model, its arguments and parameters.

Parameters:
  • model (ModelCallable) – Model used to build the table

  • arguments (List[Real]) – First argument

  • parameters (Tuple[Real, ...]) – List of the model parameters

  • arguments2 (Optional[List[Real]], optional) – Second argument

  • arg1_name (Optional[str], optional) – Name of the first argument, defaults to Index

  • arg2_name (Optional[str], optional) – Name of the first argument, defaults to Index

  • entries_name (Optional[str], optional) – Name of the first argument, defaults to Entries

  • units (Optional[List[Unit]], optional) – List of units, in the order [u_arg1, u_entries] for single argument table, [u_arg1, u_arg2, u_entries] for double argument table

Returns:

New HTable following a certain model

Return type:

HTable

get(key: float | Real | Quantity, with_unit: Literal[False]) float | Real[source] [edit on github]
get(key: float | Real, with_unit: Literal[True] = True) float | Real | Quantity
get(key: Quantity, with_unit: Literal[True] = True) 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[float | Real], method: Literal['mask', 'interpolate'] = 'mask') HTable[source] [edit on github]

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

set_index(index: str | Sequence[str], engine=None)[source] [edit on github]
shift(column: str, value: int) HTable[source] [edit on github]

Helper function to shift a column by value

Parameters:
  • column (str) – Name of the column to shift

  • value (Real) – How much to shift the column

Returns:

New shifted table

Return type:

HTable

to_pandas(index=None, use_nullable_int=True, symmetry=True) 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
unfreeze()[source] [edit on github]

Unfreezes the tables values for to_pandas and get results