pola-rs/polars · error · AttributeError

'{type(self).__name__}' object has no attribute {name!r}

Error message

'{type(self).__name__}' object has no attribute {name!r}

What it means

AttributeError from _NamespaceSuggestMixin.__getattr__ (py-polars/src/polars/_utils/various.py:749-759) for a missing attribute on a polars namespace object where no close match exists (difflib cutoff 0.6 not met). The message is the plain "'<Class>' object has no attribute '<name>'" with no suggestion, meaning the name is not a near-miss of any public method on that namespace.

Source

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

            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

  1. Look up the correct home of the method: dir(obj), help(obj), or the API docs for that namespace
  2. Call the method on the parent object if it is not namespace-scoped, e.g. df.pivot() not df.str.pivot()
  3. Upgrade/downgrade polars to the version whose API you are coding against

Example fix

# before
df.str.pivot(on='x')  # AttributeError, no suggestion

# after
df.pivot(on='x')
Defensive patterns

Strategy: type-guard

Validate before calling

import polars as pl

def method_exists(obj: object, name: str) -> bool:
    return hasattr(obj, name)

Type guard

def ns_has(ns: object, name: str) -> bool:
    return name in dir(type(ns))

Prevention

When it happens

Trigger: Calling methods that never existed on that namespace: df.str.pivot(...), s.dt.group_by(...); calling a DataFrame-level or Series-level method on the wrong sub-namespace; using a method from a different polars version.

Common situations: Assuming a method lives in a namespace it does not (e.g. window functions under .str); code written against a different polars version; tabs/autocomplete guessing.

Related errors


AI-assisted analysis of pola-rs/polars@df599052da (2026-08-16). Data as JSON: /api/errors/42ef84035c53abfe. Report an issue: GitHub.