pandas-dev/pandas · error · AssertionError
{cls_name} Expected type {cls}, found {type(left)} instead
Error message
{cls_name} Expected type {cls}, found {type(left)} instead What it means
Raised by _check_isinstance (asserters.py:162-187), the type-guard used at the top of pandas testing assertion functions (assert_index_equal, assert_series_equal, assert_frame_equal, assert_dict_equal, assert_numpy_array_equal, etc.). It raises AssertionError when the 'left' argument is not an instance of the expected class. This is a test-author error, not a data error.
Source
Thrown at pandas/_testing/asserters.py:181
"""
Helper method for our assert_* methods that ensures that
the two objects being compared have the right type before
proceeding with the comparison.
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,View on GitHub (pinned to 71959b8cb9)
Solutions
- Wrap the left argument in the expected constructor, e.g. pd.Series(left_list) or pd.Index(left_array).
- Use the matching assert function for the actual types (assert_frame_equal for DataFrames, assert_series_equal for Series).
- Inspect the error message: 'found <type>' tells you exactly what left is.
Example fix
# before assert_series_equal([1, 2, 3], pd.Series([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
left = build_left()
if not isinstance(left, pd.Series):
left = pd.Series(left)
assert_series_equal(left, expected) Type guard
import pandas as pd
def is_series(obj: object) -> bool:
return isinstance(obj, pd.Series) Prevention
- Always construct expected/actual with explicit pandas constructors in tests.
- Pick the assert_*_equal function matching the operand type.
- Run a quick isinstance(left, cls) before the assertion in helper code.
When it happens
Trigger: Calling assert_series_equal(list_of_values, series) where left is a Python list instead of a pd.Series; calling assert_index_equal on a plain array; passing a DataFrame where a Series is expected. The function is in pandas.testing and pandas._testing.
Common situations: Test helpers that build expected values as lists/dicts and forget to wrap them in the pandas constructor; refactoring a test and changing the object type on one side; comparing a numpy array with assert_numpy_array_equal but passing a list.
Related errors
- {cls_name} Expected type {cls}, found {type(right)} instead
- check_like must be False if check_index is False
- [datetimelike_compat=True] {left._values} is not equal to {r
- {obj} are different {message}
- {left_base!r} is not {right_base!r}
AI-assisted analysis of pandas-dev/pandas@71959b8cb9 (2026-08-07).
Data as JSON: /api/errors/448b2b376af40ab4.
Report an issue: GitHub.