Skip to content

Instantly share code, notes, and snippets.

@bradmontgomery
Last active September 26, 2019 22:37
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bradmontgomery/d85bb545bfe8e5dd5a0fb177ffd52d5c to your computer and use it in GitHub Desktop.
Save bradmontgomery/d85bb545bfe8e5dd5a0fb177ffd52d5c to your computer and use it in GitHub Desktop.
Pytables cheatsheet

On using pytables; how do I...

A lot of this info is in the docs for Tables.

Open the file for reading, and get a reference to a table:

>>> with tables.open_file(path, mode='r') as f:
...     table = f.root.some_columns.table

How many records are in the table?

>>> table.nrows

Search for a matching row (e.g. row matching the value 12345)

>>> table = f.root.some_table.table
>>> table.read_where('some_column==12345')
array([(12345, 3928, 364544)]
    dtype=[('some_column', '<i8'), ('foo', '<i8'), ('bar', '<i8')])

>>> for row in table.where('some_column==12345')
...     print(row['some_column'], row['foo'], row['bar'])

Extract a single column for a matching row (similar to above):

>>> arr = table.read_where('some_column=12345')
>>> bar = arr[0][2]

Find a row for a given key (column value), and update a column in that row of the table:

>>> arr = table.get_where_list('some_column==12345')
>>> key = arr[0]
>>> table.cols.bar[key] = 42

Construct a dataframe with all values

>>> arr = table.read()
>>> df = pd.DataFrame(arr)

Add an index to a column of the table:

>>> table.cols.some_column.create_index()
>>> table.reindex()  # to rebuild the index...
>>> table.reindex_dirty()  # rebuild indexes, if they are dirty.

How to add blosc compression? (See the docs on Filters)

>>> blosc_filter = Filters(complevel=9, complib='blosc')
>>> table = fileh.create_table( ... )
>>> table.cols.some_column.create_index(filters=blosc_filter)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment