import tables
# Copied from https://github.com/cta-observatory/cta-lstchain/blob/v0.10.7/lstchain/io/io.py#L828
# to remove explicit dependnecy on entire lstchain
[docs]
def add_column_table(table, ColClass, col_label, values):
"""
Add a column to an pytable Table
Parameters
----------
table: `tables.table.Table`
ColClass: `tables.atom.MetaAtom`
col_label: str
values: list or `numpy.ndarray`
Returns
-------
`tables.table.Table`
"""
# Step 1: Adjust table description
d = table.description._v_colobjects.copy() # original description
d[col_label] = ColClass() # add column
# Step 2: Create new temporary table:
newtable = tables.Table(table._v_file.root, "_temp_table", d, filters=table.filters) # new table
table.attrs._f_copy(newtable) # copy attributes
# Copy table rows, also add new column values:
for row, value in zip(table, values):
newtable.append([tuple(list(row[:]) + [value])])
newtable.flush()
# Step 3: Move temporary table to original location:
parent = table._v_parent # original table location
name = table._v_name # original table name
table.remove() # remove original table
newtable.move(parent, name) # move temporary table to original location
return newtable