{"id":2799,"library":"tables","title":"PyTables (Hierarchical Datasets)","description":"PyTables is a Python library for managing hierarchical datasets, designed for efficient handling of extremely large amounts of data. It builds on the HDF5 library and NumPy, providing high-performance I/O for scientific data. The current version is 3.11.1, and it maintains an active release cadence with regular updates.","status":"active","version":"3.11.1","language":"en","source_language":"en","source_url":"https://github.com/PyTables/PyTables","tags":["data science","hdf5","array","storage","scientific computing","large data"],"install":[{"cmd":"pip install tables","lang":"bash","label":"Install PyTables"}],"dependencies":[{"reason":"Fundamental for array operations and defining data structures.","package":"numpy","optional":false}],"imports":[{"note":"The primary module for PyTables.","symbol":"tables","correct":"import tables"},{"note":"As of PyTables 3.10.0, the `open_file` function (snake_case) is the preferred method for opening HDF5 files, aligning with Python style guides. `openFile` (camelCase) is still supported but discouraged.","wrong":"tables.openFile","symbol":"open_file","correct":"tables.open_file"},{"note":"Base class for defining the structure (description) of tables.","symbol":"IsDescription","correct":"tables.IsDescription"}],"quickstart":{"code":"import tables as tb\nimport numpy as np\nimport os\n\n# Define a table description\nclass MyTableDescription(tb.IsDescription):\n    col1 = tb.StringCol(16, pos=1)\n    col2 = tb.Int32Col(pos=2)\n    col3 = tb.Float64Col(pos=3)\n\nfilename = \"mytable.h5\"\nif os.path.exists(filename):\n    os.remove(filename)\n\ntry:\n    # Open the HDF5 file in write mode\n    with tb.open_file(filename, mode=\"w\", title=\"Test File\") as h5f:\n        # Create a group for organization\n        group = h5f.create_group(h5f.root, \"data\")\n\n        # Create a table within the group\n        table = h5f.create_table(group, 'table1', MyTableDescription, \"My First Table\")\n\n        # Append data to the table\n        table.append([(\"row_a\", 1, 1.1), (\"row_b\", 2, 2.2)])\n        table.flush() # Ensure data is written to disk\n\n        print(\"\\nData in table1 after first append:\")\n        for row in table.iterrows():\n            print(f\"  col1: {row['col1']}, col2: {row['col2']}, col3: {row['col3']}\")\n\n        # Add more data\n        table.append([(\"row_c\", 3, 3.3), (\"row_d\", 4, 4.4)])\n        table.flush()\n\n        print(\"\\nAll data in table1 (as NumPy record array):\")\n        print(table[:]) # Read all data into a NumPy record array\n\n    print(f\"\\nSuccessfully created and written to {filename}\")\n\n    # Re-open the file in read mode to verify\n    with tb.open_file(filename, mode=\"r\") as h5f_read:\n        read_table = h5f_read.root.data.table1\n        print(\"\\nData read from file:\")\n        for row in read_table.iterrows():\n            print(f\"  col1: {row['col1']}, col2: {row['col2']}, col3: {row['col3']}\")\n\nexcept Exception as e:\n    print(f\"An error occurred: {e}\")\nfinally:\n    # Clean up the created file\n    if os.path.exists(filename):\n        os.remove(filename)\n        print(f\"Cleaned up {filename}\")","lang":"python","description":"This quickstart demonstrates how to create an HDF5 file, define a table structure using `IsDescription`, create a table, append data, and read data using PyTables. It also shows the importance of using context managers (`with`) for file handling to ensure proper closing."},"warnings":[{"fix":"Update code that relies on the return value of these methods. Access the newly renamed or moved node through its parent group or by directly referring to its new path.","message":"The `File.rename_node()` and `File.move_node()` methods no longer return the new node. They now return `None`.","severity":"breaking","affected_versions":">=3.7.0"},{"fix":"Instead of `obj=MyObject`, use `description=MyDescriptionClass` with a `tables.IsDescription` subclass to define the table's structure. This improves clarity and consistency.","message":"The `obj` argument for `File.create_table()` is deprecated.","severity":"deprecated","affected_versions":">=3.10.0"},{"fix":"If specific compression ratios or performance characteristics are critical, explicitly set the `complevel` parameter within the `Filters` object when creating extendable arrays (EArray) or tables with compression enabled.","message":"The default compression level for `zlib` and `blosc` filters changed from `zlib.Z_DEFAULT_COMPRESSION` to `1`.","severity":"gotcha","affected_versions":">=3.5.0"},{"fix":"Use a `with tables.open_file(...) as h5f:` statement, which automatically handles closing the file even if errors occur. Alternatively, explicitly call `h5f.close()` when the file is no longer needed.","message":"Always ensure `tables.File` objects are properly closed to prevent data corruption, resource leaks, or incomplete writes.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-10T00:00:00.000Z","next_check":"2026-07-09T00:00:00.000Z"}