Skip to content

Instantly share code, notes, and snippets.

@danieljfarrell
Forked from swarbhanu/addColumn_PyTables.py
Created September 4, 2015 15:17
Show Gist options
  • Save danieljfarrell/b848a692e7241848bff5 to your computer and use it in GitHub Desktop.
Save danieljfarrell/b848a692e7241848bff5 to your computer and use it in GitHub Desktop.
Example showing how to add a column in PyTables
from tables import *
# Isolated the copying logic
def append_column(table, group, name, column):
"""Returns a copy of `table` with an empty `column` appended named `name`."""
description = table.description._v_colObjects.copy()
description[name] = column
copy = Table(group, table.name+"_copy", description)
# Copy the user attributes
table.attrs._f_copy(copy)
# Fill the rows of new table with default values
for i in xrange(table.nrows):
copy.row.append()
# Flush the rows to disk
copy.flush()
# Copy the columns of source table to destination
for col in descr:
getattr(copy.cols, col)[:] = getattr(table.cols, col)[:]
# store original name
#name = table.name
# Remove the original table
table.remove()
#copy.name = name
return copy
# Describe a water class
class Water(IsDescription):
waterbody_name = StringCol(16, pos=1) # 16-character String
lati = Int32Col(pos=2) # integer
longi = Int32Col(pos=3) # integer
airpressure = Float32Col(pos=4) # float (single-precision)
temperature = Float64Col(pos=5) # double (double-precision)
# Open a file in "w"rite mode
fileh = openFile("myadd-column.h5", mode = "w")
# Create a new group
group = fileh.createGroup(fileh.root, "newgroup")
# Create a new table in newgroup group
table = fileh.createTable(group, 'table', Water, "A table", Filters(1))
# Append several rows
table.append([("Atlantic", 10, 0, 10*10, 10**2),
("Pacific", 11, -1, 11*11, 11**2),
("Atlantic", 12, -2, 12*12, 12**2)])
print "Contents of the original table:", fileh.root.newgroup.table[:]
# Create another table but this time in the root directory
tableroot = fileh.createTable(fileh.root, 'root_table', Water, "A table at root", Filters(1))
# Append data...
tableroot.append([("Mediterranean", 10, 0, 10*10, 10**2),
("Mediterranean", 11, -1, 11*11, 11**2),
("Adriatic", 12, -2, 12*12, 12**2)])
# close the file
fileh.close()
# Open it again in append mode
fileh = openFile("myadd-column.h5", "a")
group = fileh.root.newgroup
table = group.table
# Get a description of table in dictionary format
descr = table.description._v_colObjects
descr2 = descr.copy()
# Add a column to description
descr2["hot"] = BoolCol(dflt=False)
table2 = append_column(table, group, "hot", BoolCol(dflt=False))
# Move table2 to table
table2.move('/newgroup','table')
# Print the new table
print "Contents of the table with column added:", fileh.root.newgroup.table[:]
# Finally, close the file
fileh.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment