Search code examples
pythonpytables

Query PyTables Nested Columns


I have a table with a nested table column, route. Beneath that are two other nested datatypes, master and slave that both have an integer id and string type field.

I would like to run something like table.readWhere('route/master/id==0') but I get "variable route refers to a nested column, not allowed in conditions"

Is there a method to query a nested datatype in pytables?


Solution

  • You have to create variables to be used inside the condition string. One option is to define a variable dictionary:

    table.readWhere('rId==0', condvars={'rId': table.cols.route.master.id})
    

    Another option is to define local variables for the columns to be used in the condition.

    rId = table.cols.route.master.id
    table.readWhere('rId==0')
    

    As this pollutes the namespace, I recommend you to create a function to wrap the code. I tried to reference the column itself, but it seems the interpreter fetches the whole dataset before throwing a NameError.

    table.readWhere('table.cols.route.master.id==0') # DOES NOT WORK
    

    More info on the where() method in the library reference.