pandas-dev/pandas · error · TypeError

Cannot compare of type to column

Error message

Cannot compare {conv_val} of type {type(conv_val)} to {kind} column

What it means

Raised in TermValue/convert_value (pytables) when the right-hand-side value of a comparison cannot be coerced to the column's declared kind. The function handles integer, float, bool, and string columns; anything else (lists, dicts, complex, datetime objects that are not handled earlier, custom types) falls through to the else branch and raises TypeError. The message reports both the value and its Python type alongside the target column kind.

Solutions

  1. Use the 'in' / 'not in' operators for membership instead of comparing to a list literal: store.select('df', where='A in [1,2,3]').
  2. Convert the RHS literal to the column's dtype before writing the query: store.select('df', where='A == 5') for an integer column.
  3. For complex/Decimal/object comparisons, read the data and filter in pandas.
  4. Verify the data_column dtype with store.get_storer('df').table.coldtypes['A'] and align your literal.

Example fix

// before
store.select('df', where='A == [1, 2, 3]')

// after
store.select('df', where='A in [1, 2, 3]')
# or for a single value
store.select('df', where='A == 1')
Defensive patterns

Strategy: validation

Validate before calling

import numpy as np

def rhs_comparable(value, kind: str) -> bool:
    if kind in ('integer', 'float'):
        return isinstance(value, (int, float, np.integer, np.floating)) and not isinstance(value, bool)
    if kind == 'bool':
        return isinstance(value, (bool, np.bool_, int, str))
    if kind == 'string':
        return isinstance(value, str)
    return False

kind = store.get_storer('df').table.coldtypes['A'].name
assert rhs_comparable(literal, kind), f'literal not comparable to {kind} column'

Type guard

def is_comparable_scalar(value, kind: str) -> bool:
    if kind in ('integer', 'float'):
        return isinstance(value, (int, float)) and not isinstance(value, bool)
    if kind == 'string':
        return isinstance(value, str)
    if kind == 'bool':
        return isinstance(value, (bool, str))
    return False

Try / catch

try:
    store.select('df', where='A == [1,2,3]')
except TypeError as e:
    if 'Cannot compare' in str(e):
        store.select('df', where='A in [1,2,3]')
    else:
        raise

Prevention

When it happens

Trigger: store.select('df', where='A == [1,2,3]') (list compared to integer column), store.select('df', where='A == 1+2j') (complex vs integer), or comparing an unsupported object type. Also when a datetime RHS slips past the datetime branch and reaches the generic else.

Common situations: Passing a Python list to emulate an 'in' filter (use 'in' operator instead). Comparing a string column to bytes, or an integer column to a Decimal that fails Decimal->int. Mismatched dtypes between the stored data_column and the literal in the where clause.

Related errors


AI-assisted analysis of pandas-dev/pandas@3b7651241d (2026-08-11). Data as JSON: /api/errors/e11d465727a1b13c. Report an issue: GitHub.

Appendix: source

Thrown at pandas/core/computation/pytables.py:307

                conv_val = conv_val.strip().lower() not in [
                    "false",
                    "f",
                    "no",
                    "n",
                    "none",
                    "0",
                    "[]",
                    "{}",
                    "",
                ]
            else:
                conv_val = bool(conv_val)
            return TermValue(conv_val, conv_val, kind)
        elif isinstance(conv_val, str):
            # string quoting
            return TermValue(conv_val, stringify(conv_val), "string")
        else:
            raise TypeError(
                f"Cannot compare {conv_val} of type {type(conv_val)} to {kind} column"
            )

    def convert_values(self) -> None:
        pass


class FilterBinOp(BinOp):
    filter: tuple[Any, Any, Index] | None = None

    def __repr__(self) -> str:
        if self.filter is None:
            return "Filter: Not Initialized"
        return pprint_thing(f"[Filter : [{self.filter[0]}] -> [{self.filter[1]}]")

    def invert(self) -> Self:
        """invert the filter"""
        if self.filter is not None:

View on GitHub (pinned to 3b7651241d)