{"record":{"id":"58cffe351a9be07f","repo":"pandas-dev/pandas","slug":"where-must-be-passed-as-a-string-pytablesexpr-or","errorCode":null,"errorMessage":"where must be passed as a string, PyTablesExpr, or list-like of PyTablesExpr","messagePattern":"where must be passed as a string, PyTablesExpr, or list-like of PyTablesExpr","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"pandas/core/computation/pytables.py","lineNumber":548,"sourceCode":"    \"\"\"\n    Validate that the where statement is of the right type.\n\n    The type may either be String, Expr, or list-like of Exprs.\n\n    Parameters\n    ----------\n    w : String term expression, Expr, or list-like of Exprs.\n\n    Returns\n    -------\n    where : The original where clause if the check was successful.\n\n    Raises\n    ------\n    TypeError : An invalid data type was passed in for w (e.g. dict).\n    \"\"\"\n    if not (isinstance(w, (PyTablesExpr, str)) or is_list_like(w)):\n        raise TypeError(\n            \"where must be passed as a string, PyTablesExpr, \"\n            \"or list-like of PyTablesExpr\"\n        )\n\n    return w\n\n\nclass PyTablesExpr(expr.Expr):\n    \"\"\"\n    Hold a pytables-like expression, comprised of possibly multiple 'terms'.\n\n    Parameters\n    ----------\n    where : string term expression, PyTablesExpr, or list-like of PyTablesExprs\n    queryables : a \"kinds\" map (dict of column name -> kind), or None if column\n        is non-indexable\n    encoding : an encoding that will encode the query terms\n","sourceCodeStart":530,"sourceCodeEnd":566,"githubUrl":"https://github.com/pandas-dev/pandas/blob/3b7651241d4da534b3559b60ef128e1c34f54116/pandas/core/computation/pytables.py#L530-L566","documentation":"Raised by _validate_where when the `where` argument to a PyTablesExpr / HDFStore.select is not a str, a PyTablesExpr, or a list-like of those. The pytables query engine needs a string predicate (or a composable PyTablesExpr), so unsupported types (dict, int, None, a single scalar) are rejected early with a TypeError.","triggerScenarios":"Calling store.select('df', where={'A': 5}) (passing a dict instead of a query string); passing where=5 or where=None; passing a single bare column name that is not list-like; passing a numpy array of PyTablesExpr that is not recognised as list-like.","commonSituations":"Confusing the pytables where API (which takes a query string like 'A>5') with boolean-indexing dicts or DataFrame.query kwargs; migrating code from dict-based filters; passing a Python set or tuple expecting it to be treated as a value list (it is list-like, but the elements must be PyTablesExpr).","solutions":["Pass where as a query string: store.select('df', where=\"A > 5\").","To combine multiple clauses, pass a list-like of PyTablesExpr or join strings with '&': where=\"(A>5) & (B<2)\".","If you have a dict of column->value, convert it to a query string first (e.g. ' & '.join(f'{k}=={v!r}' for k,v in d.items()))."],"exampleFix":"// before\nstore.select('df', where={'A': 5, 'B': 2})\n\n// after\nstore.select('df', where=\"A == 5 & B == 2\")","handlingStrategy":"type-guard","validationCode":"from pandas.core.dtypes.common import is_list_like\nfrom pandas.core.computation.pytables import PyTablesExpr\n\ndef valid_where(w):\n    return isinstance(w, (str, PyTablesExpr)) or (is_list_like(w) and all(isinstance(x, (str, PyTablesExpr)) for x in w))","typeGuard":"from pandas.core.computation.pytables import PyTablesExpr\nfrom pandas.core.dtypes.common import is_list_like\n\ndef is_valid_where(w) -> bool:\n    if isinstance(w, (str, PyTablesExpr)):\n        return True\n    return is_list_like(w) and all(isinstance(x, (str, PyTablesExpr)) for x in w)","tryCatchPattern":"try:\n    store.select('df', where=w)\nexcept TypeError as err:\n    if 'where must be passed' in str(err):\n        w = ' & '.join(f'{k}=={v!r}' for k, v in w.items()) if isinstance(w, dict) else str(w)\n        store.select('df', where=w)\n    else:\n        raise","preventionTips":["Always build where clauses as query strings, never dicts.","Validate where with is_valid_where before calling select in library code.","Document the where contract in your wrapper functions."],"tags":["pytables","hdf5","type-validation","where-clause","query"],"backgroundTag":null,"analyzedSha":"3b7651241d4da534b3559b60ef128e1c34f54116","analyzedAt":"2026-08-11T22:10:44.015Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}