pola-rs/polars · error · TypeError

expected `other` to be a

Error message

expected `other` to be a {qualified_type_name(current)!r}, not {qualified_type_name(other)!r}

What it means

Polars' `require_same_type` guard raises TypeError when an operation is given an `other` argument whose Python class differs from `current` (e.g. passing a DataFrame where a LazyFrame is expected, or vice versa). The message names both qualified types so the mismatch is obvious. It is a fail-fast check for API misuse that would otherwise surface as confusing Rust-side panics.

Solutions

  1. Check `type(other)` and convert it: wrap in `.lazy()` for LazyFrame parameters or call `.collect()` for DataFrame parameters
  2. Ensure you imported and constructed the correct Polars class (DataFrame vs LazyFrame)
  3. If `other` comes from another library, convert first (e.g. `pl.from_pandas(pdf)`, `pl.from_arrow(tbl)`)

Example fix

// before
lf.join(other_df, on="id")  # TypeError: expected LazyFrame, not DataFrame
// after
lf.join(other_df.lazy(), on="id")
Defensive patterns

Strategy: type-guard

Validate before calling

assert isinstance(other, type(df)), f"expected {type(df).__name__}, got {type(other).__name__}"

Type guard

def is_same_polars_type(current: object, other: object) -> bool:
    return isinstance(other, type(current)) or isinstance(current, type(other))

Try / catch

try:
    result = df.join(other, on="id")
except TypeError as e:
    if "expected `other`" in str(e):
        other = other.lazy() if isinstance(df, pl.LazyFrame) else other.collect()
        result = df.join(other, on="id")
    else:
        raise

Prevention

When it happens

Trigger: Calling `df.equals(other)`, `lf.join(other, ...)`, `join_asof`, `join_where`, `df.vstack(other)`, or `df.extend(other)` where `other` is not an instance of the same class (or a subclass/superclass relationship) as the receiver.

Common situations: Mixing DataFrame and LazyFrame objects after an accidental `.lazy()` or `.collect()`; passing a PyArrow table or Pandas DataFrame instead of a Polars object; holding a mock or subclass wrapper in tests.

Understand the failure class

Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.

Related errors


AI-assisted analysis of pola-rs/polars@fe841f959e (2026-09-18). Data as JSON: /api/errors/591e84326e2f53c5. Report an issue: GitHub.

Appendix: source

Thrown at py-polars/src/polars/_utils/various.py:741

def require_same_type(current: Any, other: Any) -> None:
    """
    Raise an error if the two arguments are not of the same type.

    The check will not raise an error if one object is of a subclass of the other.

    Parameters
    ----------
    current
        The object the type of which is being checked against.
    other
        An object that has to be of the same type.
    """
    if not isinstance(other, type(current)) and not isinstance(current, type(other)):
        msg = (
            f"expected `other` to be a {qualified_type_name(current)!r}, "
            f"not {qualified_type_name(other)!r}"
        )
        raise TypeError(msg)


class _NamespaceSuggestMixin:
    """Mixin that adds suggestions to AttributeError on namespace typos."""

    def __getattr__(self, name: str) -> NoReturn:
        import difflib

        public = [m for m in dir(type(self)) if not m.startswith("_")]
        matches = difflib.get_close_matches(name, public, n=1, cutoff=0.6)
        if matches:
            msg = f"'{type(self).__name__}' object has no attribute {name!r}. Did you mean: {matches[0]!r}?"
        else:
            msg = f"'{type(self).__name__}' object has no attribute {name!r}"
        raise AttributeError(msg)

View on GitHub (pinned to fe841f959e)