Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create HDF5 Group /Table if it does not exist

I am building an HDF5 file using PyTables python package. The file would be updated everyday with latest tick data. I want to create two groups - Quotes and Trades and tables for different futures expiries. I want to check if the group Quotes exists or not and if not then create it. What is the best way to do it in PyTables?

Here is a code snippet of where I am right now:

hdf_repos_filters = tables.Filters(complevel=1, complib='zlib')
for instrument in instruments:
    if options.verbose:
    hdf_file = os.path.join(dest_path, "{}.h5".format(instrument))
    store = tables.open_file(hdf_file, mode='a', filters=hdf_repos_filters)
    # This is where I want to check whether the group "Quotes" and "Trades" exist and if not create it
like image 223
Kapil Sharma Avatar asked Oct 28 '25 07:10

Kapil Sharma


2 Answers

Kapil is on the right track in that you want to use the __contains__ method, although because it is a double underscore method it is not intended to be called directly rather through an alternate interface. In this case that interface is in. So to check a file hdf_file contains a group "Quotes" you can run:

with tables.open_file(hdf_file) as store:
    if "/Quotes" in store:
       print(f"Quotes already exists in the file {hdf_file}")
like image 126
Malcolm Ramsay Avatar answered Oct 31 '25 12:10

Malcolm Ramsay


I think I have figured it out.

I am using the File.__contains__(path) method in the File class in PyTables.

As per the documentation:

File.__contains__(path)

Is there a node with that path?

Returns True if the file has a node with the given path (a string), False otherwise.

PyTables File class

like image 21
Kapil Sharma Avatar answered Oct 31 '25 13:10

Kapil Sharma