pola-rs/polars · error · TypeError
escape_regex function is unsupported for `Expr`, you may…
Error message
escape_regex function is unsupported for `Expr`, you may want use `Expr.str.escape_regex` instead
What it means
The standalone `polars.escape_regex` function operates on plain Python strings only. Passing a polars Expr raises this TypeError pointing to the expression-level method `Expr.str.escape_regex`, which is the correct API for expressions.
Solutions
- Use the method form: pl.col('text').str.escape_regex() inside expressions
- Reserve pl.escape_regex(...) for plain Python str values
- Split the pipeline: escape plain strings before building expressions
Example fix
// before
df.select(pl.escape_regex(pl.col('txt')))
// after
df.select(pl.col('txt').str.escape_regex()) Defensive patterns
Strategy: type-guard
Validate before calling
if isinstance(value, pl.Expr):
value = value.str.escape_regex()
else:
value = pl.escape_regex(value) Type guard
def is_polars_expr(x) -> bool:
return isinstance(x, pl.Expr) Try / catch
try:
escaped = pl.escape_regex(val)
except TypeError as e:
if 'Expr' in str(e):
escaped = val.str.escape_regex()
else:
raise Prevention
- Use .str.escape_regex on columns, pl.escape_regex only on plain strings
- Search codebase for pl.escape_regex(pl.col pattern in reviews
- Keep string-cleaning helpers split into scalar vs expression variants
When it happens
Trigger: Calling pl.escape_regex(pl.col('text')) or pl.escape_regex(any expression) inside a select/with_columns context.
Common situations: Misreading the docs and using the free function inside an expression context; migrating code from a plain-string helper into a DataFrame pipeline unchanged.
Understand the failure class
Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.
Related errors
- escape_regex function supports only `str` type, got
- "extract_groups" expects a `str`, given a
- from_lazyframe.resolver(): expected instance of…
- string URI is invalid here; call `read_database_uri` instead
- {0}
AI-assisted analysis of pola-rs/polars@fe841f959e (2026-09-18).
Data as JSON: /api/errors/10632f078f8b230e.
Report an issue: GitHub.
Appendix: source
Thrown at py-polars/src/polars/functions/escape_regex.py:24
with contextlib.suppress(ImportError): # Module not available when building docs
import polars._plr as plr
import polars._reexport as pl
def escape_regex(s: str) -> str:
r"""
Escapes string regex meta characters.
Parameters
----------
s
The string whose meta characters will be escaped.
"""
if isinstance(s, pl.Expr):
msg = "escape_regex function is unsupported for `Expr`, you may want use `Expr.str.escape_regex` instead"
raise TypeError(msg)
elif not isinstance(s, str):
msg = f"escape_regex function supports only `str` type, got `{qualified_type_name(s)}`"
raise TypeError(msg)
return plr.escape_regex(s)
View on GitHub (pinned to fe841f959e)