Last active
September 26, 2019 22:37
-
-
Save bradmontgomery/d85bb545bfe8e5dd5a0fb177ffd52d5c to your computer and use it in GitHub Desktop.
Revisions
-
bradmontgomery revised this gist
Sep 26, 2019 . No changes.There are no files selected for viewing
-
bradmontgomery revised this gist
Mar 4, 2019 . 1 changed file with 43 additions and 28 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 ``` 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](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) ``` -
bradmontgomery revised this gist
Mar 4, 2019 . 1 changed file with 5 additions and 3 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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). 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? -
bradmontgomery created this gist
Mar 4, 2019 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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)