{"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/71959b8cb9b2459c16e14b34f28b178ccfe14735/pandas/core/computation/pytables.py#L530-L566","documentation":"Raised by _validate_where in pandas.core.computation.pytables when the 'where' argument passed to PyTablesExpr (and thus to HDFStore.select / read_hdf) is not a str, not a PyTablesExpr, and not a list-like of those types. The validator explicitly allows str, PyTablesExpr, or is_list_like(w); anything else (dict, int, None used incorrectly, custom objects) is rejected with TypeError.","triggerScenarios":"store.select('df', where={'a': 5}); store.select('df', where=123); store.select('df', where=None) when None is not a valid where; passing a numpy boolean array directly instead of an expression.","commonSituations":"Confusing the pytables where API with the DataFrame[...] boolean-mask API; passing a dict of conditions; passing raw arrays or scalars; assuming None means 'no filter' (it does not - omit the where argument instead).","solutions":["Pass a string expression: store.select('df', where='a > 5').","Pass a PyTablesExpr built once and reused: expr = pd.core.computation.pytables.PyTablesExpr('a > 5', queryables=...); store.select('df', where=expr).","Pass a list-like of expressions: where=['a > 5', 'b < 3'].","To select all rows, omit where entirely: store.select('df').","If you have a boolean mask, read the frame first then apply it: df[mask]."],"exampleFix":"# before\nstore.select('df', where={'a': 5})  # TypeError: where must be passed as a string ...\n\n# after (string)\nstore.select('df', where='a == 5')\n# after (omit where to select all)\nstore.select('df')\n# after (boolean mask in pandas)\ndf = store.get('df')\ndf[df['a'] == 5]","handlingStrategy":"type-guard","validationCode":"import pandas as pd\nfrom pandas.core.computation.pytables import PyTablesExpr\nfrom pandas.core.dtypes.common import is_list_like\n\ndef assert_valid_where(w):\n    if not (isinstance(w, (str, PyTablesExpr)) or is_list_like(w)):\n        raise TypeError('where must be str, PyTablesExpr, or list-like of those')\n    return w","typeGuard":"import pandas as pd\nfrom pandas.core.computation.pytables import PyTablesExpr\nfrom pandas.core.dtypes.common import is_list_like\n\ndef is_valid_where(w) -> bool:\n    if w is None:\n        return False\n    return isinstance(w, (str, PyTablesExpr)) or is_list_like(w)\n","tryCatchPattern":"try:\n    store.select('df', where=where)\nexcept TypeError as e:\n    if 'where must be passed as a string' in str(e):\n        if where is None:\n            store.select('df')\n        else:\n            store.select('df', where=str(where))\n    else:\n        raise","preventionTips":["Pass where as a string expression or a list of them.","To select all rows, omit the where argument entirely.","Apply boolean masks in pandas after reading, not via where."],"tags":["pandas","hdf5","pytables","where","type-validation"],"analyzedSha":"71959b8cb9b2459c16e14b34f28b178ccfe14735","analyzedAt":"2026-08-07T01:30:20.476Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}