pandas-dev/pandas · error · AssertionError

[datetimelike_compat=True]

Error message

[datetimelike_compat=True] {left._values} is not equal to {right._values}.

What it means

Raised inside `assert_series_equal` when `datetimelike_compat=True` is set and the underlying values of two Series (with compatible but not identical dtypes — e.g. datetime64 vs period, or M8 vs object of datetimes) are not equal under Index equality. The compat flag is an internal escape hatch for comparing Series whose dtypes are 'compatible' (datetime-like) but represented differently.

Solutions

  1. Normalize both Series to the same dtype before asserting (`s.astype('datetime64[ns]')`).
  2. Drop `datetimelike_compat=True` if not needed and compare values directly.
  3. Inspect `left._values` vs `right._values` to find the offending element.

Example fix

// before
assert_series_equal(s_obj, s_dt, datetimelike_compat=True)

// after
assert_series_equal(s_obj.astype('datetime64[ns]'), s_dt)
Defensive patterns

Strategy: validation

Validate before calling

if datetimelike_compat:
    assert Index(left._values).equals(Index(right._values))

Prevention

When it happens

Trigger: Internal pandas tests calling `assert_series_equal` with `datetimelike_compat=True` on Series where one side holds `datetime.datetime` objects and the other holds `Timestamp`s that don't compare equal after coercion; mismatches surfaced during dtype refactors.

Common situations: Refactoring datetimelike dtype representations; comparing Series produced by different I/O paths (one as object, one as datetime64).

Related errors


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

Appendix: source

Thrown at pandas/_testing/asserters.py:1207

                obj=str(obj),
                class_obj=f"{obj} values",
                index_values=left.index,
            )
    elif check_datetimelike_compat and (
        needs_i8_conversion(left.dtype) or needs_i8_conversion(right.dtype)
    ):
        # we want to check only if we have compat dtypes
        # e.g. integer and M|m are NOT compat, but we can simply check
        # the values in that case

        # datetimelike may have different objects (e.g. datetime.datetime
        # vs Timestamp) but will compare equal
        if not Index(left._values).equals(Index(right._values)):
            msg = (
                f"[datetimelike_compat=True] {left._values} "
                f"is not equal to {right._values}."
            )
            raise AssertionError(msg)
    elif isinstance(left.dtype, IntervalDtype) and isinstance(
        right.dtype, IntervalDtype
    ):
        assert_interval_array_equal(
            cast("IntervalArray", left.array), cast("IntervalArray", right.array)
        )
    elif isinstance(left.dtype, CategoricalDtype) or isinstance(
        right.dtype, CategoricalDtype
    ):
        _testing.assert_almost_equal(
            left._values,
            right._values,
            rtol=rtol,
            atol=atol,
            check_dtype=bool(check_dtype),
            obj=str(obj),
            index_values=left.index,
        )

View on GitHub (pinned to 3b7651241d)