In [1]:
import h5py
import numpy as np
In [32]:
with h5py.File("mytestfile.hdf5", "w") as f:
dset = f.create_dataset("mydataset", (100,), dtype='i')
print dset.shape, dset.dtype
dset[...] = np.arange(100)
print dset[0:100:10], '\n'
print f.name, type(f), '\n'
print dset.name, type(dset), '\n'
grp = f.create_group("subgroup")
print grp.name, type(grp), '\n'
dset2 = grp.create_dataset("another_dataset", (50,), dtype='f')
print dset2.name, type(dset2), '\n'
dset3 = f.create_dataset('subgroup2/dataset_three', (10,), dtype='i')
print dset3.name, type(dset3), '\n'
# Attributes of the dataset
dset.attrs['temperature'] = 99.5
In [39]:
# h5py with pandas
from pandas import HDFStore, DataFrame
with HDFStore('storage.h5') as hdf:
df = DataFrame(np.random.rand(5,3), columns=('A','B','C'))
hdf.put('d1', df, format='table', data_columns=True)
print hdf['d1'].shape
hdf.append('d1', DataFrame(np.random.rand(5,3), columns=('A','B','C')),
format='table', data_columns=True)
print type(hdf['d1'])
In [42]:
from pandas import read_hdf
hdf = read_hdf('storage.h5', 'd1', where='A>.5', columns=('A', 'B'))
print hdf
In [47]:
with HDFStore('storage.h5') as hdf:
hdf.put('tables/t1', DataFrame(np.random.rand(20, 5)))
hdf.put('tables/t2', DataFrame(np.random.rand(10,3)))
hdf.put('new_tables/t1', DataFrame(np.random.rand(15,2)))
print hdf
In [56]:
from tables import *
class Particle(IsDescription):
identity = StringCol(itemsize=22, dflt=" ", pos=0) # character String
idnumber = Int16Col(dflt=1, pos = 1) # short integer
speed = Float32Col(dflt=1, pos = 2) # single-precision
fileh = open_file('objecttree.h5', mode='w')
root = fileh.root
print 'Root: ', root
group1 = fileh.create_group(root, "group1")
group2 = fileh.create_group(root, "group2")
array1 = fileh.create_array(root, "array1", ["string", "array"], "String array")
table1 = fileh.create_table(group1, "table1", Particle)
table2 = fileh.create_table("/group2", "table2", Particle)
array2 = fileh.create_array("/group1", "array2", [1,2,3,4])
print fileh
# Now, fill the tables
for table in (table1, table2):
# Get the record object associated with the table:
row = table.row
# Fill the table with 10 records
for i in xrange(10):
# First, assign the values to the Particle record
row['identity'] = 'This is particle: %2d' % (i)
row['idnumber'] = i
row['speed'] = i * 2.
# This injects the Record values
row.append()
# Flush the table buffers
table.flush()
print fileh
fileh.close()
In [ ]: