Skip to content

Instantly share code, notes, and snippets.

@bradmontgomery
Last active September 26, 2019 22:37
Show Gist options
  • Save bradmontgomery/d85bb545bfe8e5dd5a0fb177ffd52d5c to your computer and use it in GitHub Desktop.
Save bradmontgomery/d85bb545bfe8e5dd5a0fb177ffd52d5c to your computer and use it in GitHub Desktop.

Revisions

  1. bradmontgomery revised this gist Sep 26, 2019. No changes.
  2. bradmontgomery revised this gist Mar 4, 2019. 1 changed file with 43 additions and 28 deletions.
    71 changes: 43 additions & 28 deletions pytables_cheatsheet.md
    Original file line number Diff line number Diff line change
    @@ -7,47 +7,62 @@ 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
    ```

    1. How many records are in the table?
    How many records are in the table?

    >>> table.nrows
    ```
    >>> table.nrows
    ```

    2. search for a matching row (e.g. row matching the value `12345`)
    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')])
    ```
    >>> 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'])
    >>> for row in table.where('some_column==12345')
    ... print(row['some_column'], row['foo'], row['bar'])
    ```

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

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

    4. Find a row for a given key (column value), and update a column in that
    row of the table:
    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
    ```
    >>> arr = table.get_where_list('some_column==12345')
    >>> key = arr[0]
    >>> table.cols.bar[key] = 42
    ```

    5. construct a dataframe with all values
    Construct a dataframe with all values

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

    6. add an index to a column of the table:
    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.
    ```
    >>> table.cols.some_column.create_index()
    >>> table.reindex() # to rebuild the index...
    >>> table.reindex_dirty() # rebuild indexes, if they are dirty.
    ```

    7. How to add blosc compression? (See the [docs on Filters](https://www.pytables.org/usersguide/libref/helper_classes.html#the-filters-class))
    How to add blosc compression? (See the [docs on Filters](https://www.pytables.org/usersguide/libref/helper_classes.html#the-filters-class))

    >>> blosc_filter = Filters(complevel=9, complib='blosc')
    >>> table = fileh.create_table( ... )
    >>> table.cols.some_column.create_index(filters=blosc_filter)
    ```
    >>> blosc_filter = Filters(complevel=9, complib='blosc')
    >>> table = fileh.create_table( ... )
    >>> table.cols.some_column.create_index(filters=blosc_filter)
    ```
  3. bradmontgomery revised this gist Mar 4, 2019. 1 changed file with 5 additions and 3 deletions.
    8 changes: 5 additions & 3 deletions pytables_cheatsheet.md
    Original file line number Diff line number Diff line change
    @@ -2,10 +2,12 @@

    A lot of this info is in [the docs for Tables](https://www.pytables.org/usersguide/libref/structured_storage.html#the-table-class).

    0. open the file for reading, and get a reference to a table:
    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
    ```
    >>> with tables.open_file(path, mode='r') as f:
    ... table = f.root.some_columns.table
    ```

    1. How many records are in the table?

  4. bradmontgomery created this gist Mar 4, 2019.
    51 changes: 51 additions & 0 deletions pytables_cheatsheet.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,51 @@
    # On using pytables; how do I...

    A lot of this info is in [the docs for Tables](https://www.pytables.org/usersguide/libref/structured_storage.html#the-table-class).

    0. 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

    1. How many records are in the table?

    >>> table.nrows
    2. 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'])

    3. extract a single column for a matching row (similar to above):

    >>> arr = table.read_where('some_column=12345')
    >>> bar = arr[0][2]
    4. 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
    5. construct a dataframe with all values

    >>> arr = table.read()
    >>> df = pd.DataFrame(arr)
    6. 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.
    7. How to add blosc compression? (See the [docs on Filters](https://www.pytables.org/usersguide/libref/helper_classes.html#the-filters-class))

    >>> blosc_filter = Filters(complevel=9, complib='blosc')
    >>> table = fileh.create_table( ... )
    >>> table.cols.some_column.create_index(filters=blosc_filter)