pandas-dev/pandas · error · AssertionError

Expected type , found instead

Error message

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

What it means

Raised by `_check_isinstance`, a guard at the entry of pandas testing asserters (`assert_index_equal`, `assert_series_equal`, `assert_frame_equal`, `assert_dict_equal`, etc.). It fires when `left` or `right` is not an instance of the expected class `cls`, signaling the test is comparing mismatched object types rather than values. This is a programming-error-class failure in test code, not data.

Solutions

  1. Match the asserter to the object: assert_frame_equal for DataFrames, assert_series_equal for Series, assert_index_equal for Index.
  2. Drop `.values`/`.to_numpy()` so you pass the pandas object, not the underlying ndarray.
  3. Read the message's `{cls}` to pick the correct asserter function name.

Example fix

// before
pd.testing.assert_series_equal(df_a, df_b)

// after
pd.testing.assert_frame_equal(df_a, df_b)
Defensive patterns

Strategy: type-guard

Validate before calling

import pandas as pd
assert isinstance(left, pd.Series) and isinstance(right, pd.Series)

Type guard

import pandas as pd
def both_are(cls, a, b) -> bool:
    return isinstance(a, cls) and isinstance(b, cls)

Prevention

When it happens

Trigger: Calling `pd.testing.assert_series_equal(df, df2)` (passing DataFrames to a Series asserter); `assert_index_equal(df.index.values, df2.index.values)` (passing numpy arrays instead of Index); `assert_dict_equal(list_a, dict_b)`.

Common situations: Test refactors where an object type changed; copy-pasting an assertion from a Series test into a DataFrame test without changing the asserter; passing `.values`/`.to_numpy()` where the Index/Series object is required.

Related errors


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

Appendix: 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 3b7651241d)