musingsole
musingsole

Reputation: 1227

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?

Upvotes: 3

Views: 781

Answers (2)

Mr. Frobenius
Mr. Frobenius

Reputation: 324

Building on the answer by streeto, here is a quick way to get access to all the nested columns in a table when constructing a query

condvars = {k.replace('/', '__'): v for k, v in table.colinstances.items()}
result = table.read_where('route__master__id == 0', condvars=condvars)

table.colinstances returns a flat dict whose keys are slash separated paths to all columns (including nested ones) in the table, and whose values are instances of the PyTables Col class located at that path. You can't use the slash separated path in the query, but if you replace with some other separator that is allowed within Python identifiers (in this case I chose double underscores), then everything works fine. You could choose some other separator if you like.

Upvotes: 0

streeto
streeto

Reputation: 123

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.

Upvotes: 6

Related Questions