pandas-dev/pandas · error · ValueError

Provide an even amount of arguments as option_context(pat…

Error message

Provide an even amount of arguments as option_context(pat, val, pat, val...).

What it means

Raised by the `option_context` context manager when, after flattening a single-dict argument, the positional args are odd in number or fewer than 2. `option_context` expects `(pat, val, pat, val, ...)` pairs (or one dict) so it can snapshot the current values, apply the new ones for the `with` block, and restore them on exit.

Solutions

  1. Pass key/value pairs: `with pd.option_context('display.max_rows', 10): ...`.
  2. Pass a single dict: `with pd.option_context({'display.max_rows': 10, 'display.max_columns': 5}): ...`.
  3. Ensure the total arg count (after dict flattening) is even and >= 2.

Example fix

# before
with pd.option_context('display.max_rows'):
    ...

# after
with pd.option_context('display.max_rows', 10):
    ...
# or
with pd.option_context({'display.max_rows': 10}):
    ...
Defensive patterns

Strategy: validation

Validate before calling

def option_context_args(*args):
    if len(args) == 1 and isinstance(args[0], dict):
        args = tuple(kv for item in args[0].items() for kv in item)
    if len(args) % 2 != 0 or len(args) < 2:
        raise ValueError('option_context needs (pat, val)+ pairs or a single dict')
    return args
pairs = option_context_args(*user_args)

Type guard

null

Try / catch

try:
    with pd.option_context(*ctx_args):
        ...
except ValueError as e:
    if 'even amount' in str(e):
        # rebuild ctx_args as pairs/dict and retry
        ...
    raise

Prevention

When it happens

Trigger: `with pd.option_context('display.max_rows'):` (value missing), `with pd.option_context('a', 1, 'b'):` (unbalanced), or `with pd.option_context():` (empty).

Common situations: Forgetting a value; refactoring that drops an arg; passing a single string instead of a pair.

Related errors


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

Appendix: source

Thrown at pandas/_config/config.py:507

    Notes
    -----
    For all available options, please view the :ref:`User Guide <options.available>`
    or use ``pandas.describe_option()``.

    Examples
    --------
    >>> from pandas import option_context
    >>> with option_context("display.max_rows", 10, "display.max_columns", 5):
    ...     pass
    >>> with option_context({"display.max_rows": 10, "display.max_columns": 5}):
    ...     pass
    """
    if len(args) == 1 and isinstance(args[0], dict):
        args = tuple(kv for item in args[0].items() for kv in item)

    if len(args) % 2 != 0 or len(args) < 2:
        raise ValueError(
            "Provide an even amount of arguments as "
            "option_context(pat, val, pat, val...)."
        )

    ops = tuple(zip(args[::2], args[1::2], strict=True))
    undo: tuple[tuple[Any, Any], ...] = ()
    try:
        undo = tuple((pat, get_option(pat)) for pat, val in ops)
        for pat, val in ops:
            set_option(pat, val)
        yield
    finally:
        for pat, val in undo:
            set_option(pat, val)


def register_option(
    key: str,

View on GitHub (pinned to 3b7651241d)