{"record":{"id":"e11d465727a1b13c","repo":"pandas-dev/pandas","slug":"cannot-compare-conv-val-of-type-type-conv-val","errorCode":null,"errorMessage":"Cannot compare {conv_val} of type {type(conv_val)} to {kind} column","messagePattern":"Cannot compare (.+?) of type (.+?) to (.+?) column","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"pandas/core/computation/pytables.py","lineNumber":307,"sourceCode":"                conv_val = conv_val.strip().lower() not in [\n                    \"false\",\n                    \"f\",\n                    \"no\",\n                    \"n\",\n                    \"none\",\n                    \"0\",\n                    \"[]\",\n                    \"{}\",\n                    \"\",\n                ]\n            else:\n                conv_val = bool(conv_val)\n            return TermValue(conv_val, conv_val, kind)\n        elif isinstance(conv_val, str):\n            # string quoting\n            return TermValue(conv_val, stringify(conv_val), \"string\")\n        else:\n            raise TypeError(\n                f\"Cannot compare {conv_val} of type {type(conv_val)} to {kind} column\"\n            )\n\n    def convert_values(self) -> None:\n        pass\n\n\nclass FilterBinOp(BinOp):\n    filter: tuple[Any, Any, Index] | None = None\n\n    def __repr__(self) -> str:\n        if self.filter is None:\n            return \"Filter: Not Initialized\"\n        return pprint_thing(f\"[Filter : [{self.filter[0]}] -> [{self.filter[1]}]\")\n\n    def invert(self) -> Self:\n        \"\"\"invert the filter\"\"\"\n        if self.filter is not None:","sourceCodeStart":289,"sourceCodeEnd":325,"githubUrl":"https://github.com/pandas-dev/pandas/blob/71959b8cb9b2459c16e14b34f28b178ccfe14735/pandas/core/computation/pytables.py#L289-L325","documentation":"Raised by BinOp.convert_value in pandas.core.computation.pytables when the right-hand comparison value cannot be coerced to the column's kind. The function handles datetime, timedelta, category, integer, float, bool, and string columns; anything else (e.g. a list, dict, complex, bytes, or an object that is not a str) falls through to the final TypeError. The {kind} placeholder shows what the column actually is, and {type(conv_val)} shows the offending Python type.","triggerScenarios":"store.select('df', where='cat_col == [1,2]') (list vs single value handling edge); comparing an integer column to a Python complex or bytes object; passing a None to a non-nullable column kind; comparing a string column to a non-str object.","commonSituations":"Programmatic where-clause construction where the comparison value comes from untrusted/dynamic input; mismatched dtypes between the stored column and the query literal; passing numpy scalars of unusual dtypes (e.g. np.complex128).","solutions":["Match the literal type to the column kind: use plain Python int/float/str/bool, or a pandas.Timestamp for datetime columns.","For membership (multiple values), use the 'in'/'==' with a list literal that the FilterBinOp path handles: where='col == [1,2,3]' only when col is a data_column.","Cast the value before passing: int(v), float(v), str(v), or pd.Timestamp(v) for datetimes.","If the value can be None, handle NaN explicitly (e.g. store with nullable dtype and query 'col != col' for NaN)."],"exampleFix":"# before\nstore.select('df', where='amount == 1.5j')  # TypeError: Cannot compare 1.5j of type complex to float column\n\n# after (cast to the column's kind)\nstore.select('df', where='amount == 1.5')   # float column -> float literal\n# for datetime columns:\nstore.select('df', where=\"ts == Timestamp('2020-01-01')\")","handlingStrategy":"type-guard","validationCode":"import numpy as np\n\ndef coerce_query_value(value, kind):\n    if kind in ('integer',):\n        return int(value)\n    if kind in ('float',):\n        return float(value)\n    if kind in ('bool',):\n        return bool(value)\n    if kind in ('datetime',) or (kind or '').startswith('datetime64'):\n        import pandas as pd\n        return pd.Timestamp(value)\n    if isinstance(value, str):\n        return value\n    raise TypeError(f'cannot coerce {value!r} for kind {kind!r}')","typeGuard":"import numpy as np\n\ndef is_comparable_scalar(v) -> bool:\n    return isinstance(v, (int, float, bool, str, np.integer, np.floating, np.bool_))\n","tryCatchPattern":"try:\n    store.select('df', where=f'col == {value!r}')\nexcept TypeError as e:\n    if 'Cannot compare' in str(e):\n        # cast value to the column's kind and retry\n        value = coerce_query_value(value, kind)\n        store.select('df', where=f'col == {value!r}')\n    raise","preventionTips":["Match the literal type to the stored column kind (int/float/bool/str/Timestamp).","Avoid passing complex, bytes, dict, or list values as comparison literals.","Cast dynamic values explicitly before building the where string."],"tags":["pandas","hdf5","pytables","type-mismatch","where"],"analyzedSha":"71959b8cb9b2459c16e14b34f28b178ccfe14735","analyzedAt":"2026-08-07T01:30:20.476Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}