pola-rs/polars · error

input string does not contain DataFrame or Series

Error message

input string does not contain DataFrame or Series

What it means

from_repr parses a polars DataFrame/Series textual repr; when neither the DataFrame pattern nor the Series regex matches the input string it raises ValueError — the string is not a polars repr that can be reconstructed.

Solutions

  1. Copy the repr verbatim from a Polars `print(df)` output, including the `shape: ...` header line and full borders.
  2. Ensure nothing was truncated: use sufficient terminal width or `pl.Config.set_tbl_width_chars`/`set_tbl_rows` to print without ellipsis.
  3. If the string is not a Polars repr, parse it another way (e.g. write/read CSV instead).

Example fix

// before
pl.from_repr('a | b\n1 | 2')
// after
pl.from_repr('shape: (1, 2)\n┌─────┬─────┐\n│ a   ┆ b   │\n│ --- ┆ --- │\n│ i64 ┆ i64 │\n╞═════╪═════╡\n│ 1   ┆ 2   │\n└─────┴─────┘')
Defensive patterns

Strategy: validation

Validate before calling

if not (s.startswith('shape:') or '┌' in s or '+---' in s):
    raise ValueError('input is not a polars DataFrame/Series repr')

Type guard

def looks_like_polars_repr(s: str) -> bool:
    return ('shape:' in s) or ('┌' in s and '└' in s) or ('+---' in s)

Try / catch

try:
    df = pl.from_repr(s)
except ValueError:
    df = pl.read_json(io.StringIO(s))  # alternate parse path

Prevention

When it happens

Trigger: Calling `pl.from_repr(s)` where `s` is truncated, has altered borders/column widths, is a different table style, or is not a Polars repr at all (e.g. markdown, pandas repr).

Common situations: Copy-pasting from logs where long lines were wrapped or ellipsized; repr printed with `PL_FMT_STR_LEN` or console options that changed formatting; copying a pandas to_string() output by mistake.

Understand the failure class

Background: "Invalid ... format", "must be in format X", "does not look like a ..." — invalid argument format errors across CLI tools and libraries — this error's family across 17 libraries.

Related errors


AI-assisted analysis of pola-rs/polars@fe841f959e (2026-09-18). Data as JSON: /api/errors/5742f47d46fb964b. Report an issue: GitHub.

Appendix: source

Thrown at py-polars/src/polars/convert/general.py:906

    ... )
    >>> 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 format

View on GitHub (pinned to fe841f959e)