pandas-dev/pandas · error · AssertionError

{cls_name} Expected type {cls}, found {type(right)} instead

Error message

{cls_name} Expected type {cls}, found {type(right)} instead

What it means

Raised by _check_isinstance (asserters.py:184-187) when the 'right' argument to a pandas.testing assertion function is not an instance of the expected class. Symmetric twin of the left-side check; same cause and audience — a test-author type mismatch on the second (usually 'expected') operand.

Source

Thrown at pandas/_testing/asserters.py:185

    Parameters
    ----------
    left : The first object being compared.
    right : The second object being compared.
    cls : The class type to check against.

    Raises
    ------
    AssertionError : Either `left` or `right` is not an instance of `cls`.
    """
    cls_name = cls.__name__

    if not isinstance(left, cls):
        raise AssertionError(
            f"{cls_name} Expected type {cls}, found {type(left)} instead"
        )
    if not isinstance(right, cls):
        raise AssertionError(
            f"{cls_name} Expected type {cls}, found {type(right)} instead"
        )


def assert_dict_equal(left: dict, right: dict, compare_keys: bool = True) -> None:
    _check_isinstance(left, right, dict)
    _testing.assert_dict_equal(left, right, compare_keys=compare_keys)


@set_module("pandas.testing")
def assert_index_equal(
    left: Index,
    right: Index,
    exact: bool | str | lib.NoDefault = lib.no_default,
    check_names: bool = True,
    check_exact: bool = True,
    check_categorical: bool = True,
    check_order: bool = True,

View on GitHub (pinned to 71959b8cb9)

Solutions

  1. Wrap the right argument in the expected constructor: pd.Series(right_list), pd.Index(right_array), pd.DataFrame(right_dict).
  2. Pick the assertion function matching the right operand's type.
  3. Read the 'found <type>' portion of the message to identify the mismatched object.

Example fix

# before
assert_series_equal(pd.Series([1, 2, 3]), [1, 2, 3])

# after
assert_series_equal(pd.Series([1, 2, 3]), pd.Series([1, 2, 3]))
Defensive patterns

Strategy: type-guard

Validate before calling

import pandas as pd
from pandas.testing import assert_series_equal
right = build_expected()
if not isinstance(right, pd.Series):
    right = pd.Series(right)
assert_series_equal(left, right)

Type guard

import pandas as pd

def is_series(obj: object) -> bool:
    return isinstance(obj, pd.Series)

Prevention

When it happens

Trigger: Calling assert_series_equal(pd.Series([...]), np.array([...])) where right is a numpy array; assert_index_equal(index, list_of_labels); assert_frame_equal(df, dict_of_columns). Any pandas.testing assert_*_equal with a right operand of the wrong type.

Common situations: Building the expected fixture as a dict/list/array and forgetting to wrap it; mock objects or third-party types substituted for the real pandas object; copy-paste of a test that compared lists now compared against Series.

Related errors


AI-assisted analysis of pandas-dev/pandas@71959b8cb9 (2026-08-07). Data as JSON: /api/errors/63baf886f1a2fbda. Report an issue: GitHub.