pandas-dev/pandas · error · KeyError

Invalid parser ' ' passed, valid parsers are

Error message

Invalid parser '{parser}' passed, valid parsers are {PARSERS.keys()}

What it means

Raised by _check_parser() when the parser argument is not one of the keys in PARSERS, which are {'python', 'pandas'} (see expr.py:866). The parser controls how the expression string is converted to an AST before evaluation; an unknown parser string means pandas cannot select a visitor class. Note the error message prints the dict_keys object rather than a list, which can look unusual in logs.

Solutions

  1. Use one of the two valid parsers: parser='pandas' (default, allows 'and'/'or'/'not' and '@' locals) or parser='python' (strict Python semantics).
  2. If the parser name comes from config or a variable, validate it against {'pandas','python'} before the call.
  3. Check for leading/trailing whitespace or accidental capitalization in the parser string.

Example fix

// before
pd.eval('a + b', parser='Python')

// after
pd.eval('a + b', parser='python')
Defensive patterns

Strategy: validation

Validate before calling

VALID_PARSERS = {'pandas', 'python'}
if parser not in VALID_PARSERS:
    raise ValueError(f'parser must be one of {VALID_PARSERS}, got {parser!r}')
pd.eval(expr, parser=parser)

Type guard

def is_valid_parser(p: str) -> bool:
    return isinstance(p, str) and p in {'pandas', 'python'}

Try / catch

try:
    pd.eval(expr, parser=parser)
except KeyError as e:
    # invalid parser
    raise ValueError(f'Bad parser: {parser}') from e

Prevention

When it happens

Trigger: Calling pd.eval(expr, parser='numpy') or any typo such as parser='Python' (capital P) or parser='py'. Also triggered by passing a parser value loaded from config that was misspelled.

Common situations: Case-sensitivity mistakes ('Python' vs 'python'); copy-paste from documentation that abbreviated the option; dynamic code that builds the parser name from a variable with a typo.

Related errors


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

Appendix: source

Thrown at pandas/core/computation/eval.py:97

    return engine


def _check_parser(parser: str) -> None:
    """
    Make sure a valid parser is passed.

    Parameters
    ----------
    parser : str

    Raises
    ------
    KeyError
      * If an invalid parser is passed
    """
    if parser not in PARSERS:
        raise KeyError(
            f"Invalid parser '{parser}' passed, valid parsers are {PARSERS.keys()}"
        )


def _check_resolvers(resolvers) -> None:
    if resolvers is not None:
        for resolver in resolvers:
            if not hasattr(resolver, "__getitem__"):
                name = type(resolver).__name__
                raise TypeError(
                    f"Resolver of type '{name}' does not "
                    "implement the __getitem__ method"
                )


def _check_expression(expr) -> None:
    """
    Make sure an expression is not an empty string

View on GitHub (pinned to 3b7651241d)