Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Append data to existing pytables table

I am new to PyTables and implemented a few basic techniques of inserting and retrieving data from a table in Pytables. However, I am not sure about how to insert data in an existing table of PyTables because all I read/get in the tutorial is creating a new table (using h5file.createTable() method). Here is the basic code that tutorial has about inserting data into PytTables table created from scratch:

h5file = openFile("tutorial1.h5", mode = "w", title = "Test file")
group = h5file.createGroup("/", 'detector', 'Detector information')
table = h5file.createTable(group, 'readout', Particle, "Readout example")

for i in xrange(10):
    particle['name']  = 'Particle: %6d' % (i)
    particle['TDCcount'] = i % 256
    particle['ADCcount'] = (i * 256) % (1 << 16)
    particle['grid_i'] = i
    particle['grid_j'] = 10 - i
    particle['pressure'] = float(i*i)
    particle['energy'] = float(particle['pressure'] ** 4)
    particle['idnumber'] = i * (2 ** 34)
    # Insert a new particle record
    particle.append()

    table.flush()

P.S. There is one place in this tutorial that talks about appending data to an existing table, but uses the table that was created from scratch and basically gives no idea about selecting pre-existing table for appending data. Kindly help. Thanks.

like image 761
khan Avatar asked May 28 '13 05:05

khan


People also ask

How do you append to a table in Python?

append() function is used to append rows of other dataframe to the end of the given dataframe, returning a new dataframe object. Columns not in the original dataframes are added as new columns and the new cells are populated with NaN value.

What is PyTables package?

PyTables is a package for managing hierarchical datasets and designed to efficiently and easily cope with extremely large amounts of data. You can download PyTables and use it for free. You can access documentation, some examples of use and presentations here.


1 Answers

You need open your file in append mode "a". Also do not create the group and table again. This appends another 10 rows:

import tables


class Particle(tables.IsDescription):
    name      = tables.StringCol(16)   # 16-character String
    idnumber  = tables.Int64Col()      # Signed 64-bit integer
    ADCcount  = tables.UInt16Col()     # Unsigned short integer
    TDCcount  = tables.UInt8Col()      # unsigned byte
    grid_i    = tables.Int32Col()      # 32-bit integer
    grid_j    = tables.Int32Col()      # 32-bit integer
    pressure  = tables.Float32Col()    # float  (single-precision)
    energy    = tables.Float64Col()    # double (double-precision)

h5file = tables.openFile("tutorial1.h5", mode = "a")
table = h5file.root.detector.readout

particle = table.row

for i in range(10, 20):
    particle['name']  = 'Particle: %6d' % (i)
    particle['TDCcount'] = i % 256
    particle['ADCcount'] = (i * 256) % (1 << 16)
    particle['grid_i'] = i
    particle['grid_j'] = 10 - i
    particle['pressure'] = float(i*i)
    particle['energy'] = float(particle['pressure'] ** 4)
    particle['idnumber'] = i * (2 ** 34)
    # Insert a new particle record
    particle.append()

h5file.close()
like image 82
Mike Müller Avatar answered Sep 20 '22 18:09

Mike Müller