pola-rs/polars · error · AttributeError

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

Error message

'{type(self).__name__}' object has no attribute {name!r}. Did you mean: {matches[0]!r}?

What it means

AttributeError from _NamespaceSuggestMixin.__getattr__ (py-polars/src/polars/_utils/various.py:749-759) when a misspelled attribute is accessed on a polars namespace object (the objects behind df.str, df.dt, etc.) and difflib.get_close_matches finds a near match (cutoff 0.6) among public attributes. The message names the class and suggests the closest attribute, e.g. "'StringNameSpace' object has no attribute 'strip_chars'. Did you mean: 'strip'?"

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. Apply the suggested attribute from the message
  2. Check the namespace's real API with dir(df.str) or help
  3. After upgrades, grep the changelog for renames of the failing method

Example fix

# before
s.str.to_lowercas()  # AttributeError: Did you mean: 'to_lowercase'?

# after
s.str.to_lowercase()
Defensive patterns

Strategy: type-guard

Validate before calling

import polars as pl

def has_attr(obj: object, name: str) -> bool:
    return name in dir(obj)

Type guard

def valid_ns_method(ns: object, name: str) -> bool:
    return not name.startswith('_') and callable(getattr(type(ns), name, None))

Try / catch

try:
    out = s.str.to_lowercas()
except AttributeError as e:
    msg = str(e)
    if 'Did you mean' in msg:
        import re
        fix = re.search(r"Did you mean: '([^']+)'", msg).group(1)
        out = getattr(s.str, fix)()
    else:
        raise

Prevention

When it happens

Trigger: Typos on namespace objects: s.str.to_lowercas(), s.dt.truncate_date(), s.list.eval_first(); renamed methods after a polars upgrade (old names no longer exist but fuzzy-match the new one).

Common situations: Upgrading polars across breaking releases where methods were renamed (the suggestion usually points at the new name); IDE-less editing; stale StackOverflow snippets.

Related errors


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