pola-rs/polars · error · ValueError
input string does not contain DataFrame or Series
Error message
input string does not contain DataFrame or Series
What it means
Raised by polars.from_repr when the input string matches neither the DataFrame table layout (found via _find_df_repr / tbl_repr_type being None) nor the Series regex (shape + 'Series:' header). from_repr reconstructs data solely by parsing the printed repr, so arbitrary text, partial copies, or differently formatted output fail both parsers and hit the terminal ValueError.
Source
Thrown at py-polars/src/polars/convert/general.py:896
... )
>>> s.to_list()
[True, False, True]
"""
# find DataFrame table...
if (tbl_repr_type := _extract_table(data)) is not None:
return _from_dataframe_repr(*tbl_repr_type)
# ...or Series in the given string
m = re.search(
pattern=r"(?:shape: (\(\d+,\))\n.*?)?Series:\s+([^\n]+)\s+\[([^\n]+)](.*)",
string=data,
flags=re.DOTALL,
)
if m is not None:
return _from_series_repr(m)
msg = "input string does not contain DataFrame or Series"
raise ValueError(msg)
def _from_dataframe_repr(tbl: str, table_repr: TableRepr) -> DataFrame:
"""Reconstruct a DataFrame from a table repr string."""
from polars.datatypes.convert import dtype_short_repr_to_dtype
from polars.io.database._inference import dtype_from_database_typename
def _dtype_from_name(tp: str | None) -> PolarsDataType | None:
return (
None
if tp is None
else (
dtype_short_repr_to_dtype(tp)
or dtype_from_database_typename(tp, raise_unmatched=False)
)
)
# associated regex patterns for the given table formatView on GitHub (pinned to df599052da)
Solutions
- Make sure the string is the verbatim output of print(df) / repr(df) or repr(s) for a pl.Series, including the border rows and the 'shape: (n, m)' header
- If the data crossed JSON/logs, repair escapes first (e.g. json.loads or codecs to unescape \\n) so newlines are real newlines
- For durable round-trips, use real serialization instead: df.write_json / pl.read_json, write_ipc, or write_parquet
- For a Series, confirm the string contains a 'Series:' line as produced by repr(series)
Example fix
# before
pl.from_repr("shape: (2, 2)\\n┌─────┐\\n│ a ─┐\\n...") # double-escaped literal
# after
import json
pl.from_repr(json.loads('"shape: (2, 2)\\n┌─────┐\\n..."')) # unescaped once, real newlines Defensive patterns
Strategy: validation
Validate before calling
import re
def looks_like_polars_repr(s: str) -> bool:
return bool(
re.search(r'shape: \(\d+,? ?\d*\)\n', s)
or re.search(r'Series:\s+[^\n]+\s+\[[^\n]+]', s)
)
if not looks_like_polars_repr(text):
raise ValueError('not a polars DataFrame/Series repr; refusing to call from_repr') Try / catch
try:
obj = pl.from_repr(text)
except ValueError as e:
if 'does not contain DataFrame or Series' in str(e):
# recover: try real serialization or surface a clear fixture error
raise AssertionError(f'fixture string is not a polars repr: {text[:80]!r}') from e
raise Prevention
- Never use from_repr as a persistence format; write_ipc/write_parquet/write_json instead
- When embedding reprs in fixtures, paste verbatim console output including borders and 'shape:' header
- Un-escape strings that traveled through JSON/logs (json.loads) before parsing
- Pin the polars version on both producer and consumer so the table format matches
When it happens
Trigger: pl.from_repr('hello world') or any prose/random string; passing str(df) of a LazyFrame or Expression instead of a DataFrame; a repr string whose borders/separator rows were mangled by copy-paste, terminal soft-wrapping, or log truncation; a repr from a polars version with a different table format.
Common situations: Copy-pasting console output of a DataFrame out of logs/notebooks into a test fixture; piping repr text through tools that strip or wrap characters; using from_repr as a serialization format in round-trip tests where the string got escaped (e.g. double-escaped \n).
Related errors
- `from_repr` does not support data type {dtype.base_type().__
- unexpected time zone offset: {offset!r}
- DataFrame should contain only String repr data; found {tp!r}
- unsupported table format: {table_repr!r}
- expected list or dict of objects
AI-assisted analysis of pola-rs/polars@df599052da (2026-08-16).
Data as JSON: /api/errors/5742f47d46fb964b.
Report an issue: GitHub.