pola-rs/polars · error · TypeError
expected `other` to be a {qualified_type_name(current)!r}, n
Error message
expected `other` to be a {qualified_type_name(current)!r}, not {qualified_type_name(other)!r} What it means
TypeError from require_same_type (py-polars/src/polars/_utils/various.py:726-744). Binary frame/series methods that mutate or combine with an `other` object (DataFrame.update, vstack/extend, Series.__iadd__-style in-place ops, LazyFrame.update/join-ish helpers — 18 call sites across frame.py, series.py, lazyframe/frame.py) require `other` to be the same type as `self` (subclass relationships allowed in either direction). Passing e.g. a pandas DataFrame, dict, list, or numpy array where a polars DataFrame/Series is required raises this with both qualified type names.
Source
Thrown at py-polars/src/polars/_utils/various.py:744
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 df599052da)
Solutions
- Convert before the call: pl.DataFrame(pandas_df) or pl.Series(values)
- Match lazy vs eager: lf.update(other.collect()) or keep both lazy
- Use the APIs designed for raw inputs (pl.DataFrame(dict), df.insert_column) instead of the binary `other` methods
Example fix
# before df.update(pandas_df) # TypeError: expected `other` to be a 'DataFrame' # after df.update(pl.DataFrame(pandas_df))
Defensive patterns
Strategy: type-guard
Type guard
import polars as pl
def is_polars_frame(o: object) -> bool:
return isinstance(o, (pl.DataFrame, pl.LazyFrame, pl.Series))
def require_frame(o: object) -> pl.DataFrame:
if not isinstance(o, pl.DataFrame):
raise TypeError(f'expected DataFrame, got {type(o).__name__}')
return o Try / catch
try:
df.update(other)
except TypeError as e:
if 'expected `other`' in str(e):
import polars as pl
df = df.update(pl.DataFrame(other))
else:
raise Prevention
- Convert foreign objects (pandas, dicts, lists) to the exact polars type before binary methods
- Keep lazy/eager pairs consistent on both sides of update/extend/vstack-style calls
- Type-annotate boundaries so mismatches surface at lint time
When it happens
Trigger: df.update(pandas_df); df_polars.vstack([row_dict]); series.extend([1, 2, 3]); lf.update(other_df) where other_df is a DataFrame instead of LazyFrame (or vice versa on the wrong call site).
Common situations: Mixed pandas/polars codebases where a variable's provenance changed; wrapping polars objects in custom containers; passing a dict of columns where a constructed frame is expected.
Related errors
- cannot select columns using key of type {qualified_type_name
- cannot select rows using key of type {qualified_type_name(ke
- cannot treat Series of type {s.dtype} as indices
- only 1D NumPy arrays can be treated as indices
- cannot treat NumPy array of type {arr.dtype} as indices
AI-assisted analysis of pola-rs/polars@df599052da (2026-08-16).
Data as JSON: /api/errors/591e84326e2f53c5.
Report an issue: GitHub.