pola-rs/polars · error · TypeError

DataFrame should contain only String repr data; found {tp!r}

Error message

DataFrame should contain only String repr data; found {tp!r}

What it means

TypeError from _cast_repr_strings_with_schema (py-polars/src/polars/_utils/various.py:326-341). This is an internal helper used by polars' repr round-trip machinery (pl.from_repr / pl.from_reprs in polars/convert/general.py): the table scraped from a text/clipboard repr is expected to contain only String columns, which are then cast to the schema inferred from the header. If any column of the parsed repr frame is not String, the invariant is broken and this error names the offending dtype.

Source

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

    Parameters
    ----------
    df
        Dataframe containing string-repr column data.
    schema
        DataFrame schema containing the desired end-state types.

    Notes
    -----
    Table repr strings are less strict (or different) than equivalent CSV data, so need
    special handling; as this function is only used for reprs, parsing is flexible.
    """
    tp: PolarsDataType | None
    if not df.is_empty():
        for tp in df.schema.values():
            if tp != String:
                msg = f"DataFrame should contain only String repr data; found {tp!r}"
                raise TypeError(msg)

    special_floats = {"-inf", "+inf", "inf", "nan"}

    # duration string scaling
    ns_sec = 1_000_000_000
    duration_scaling = {
        "ns": 1,
        "us": 1_000,
        "µs": 1_000,
        "ms": 1_000_000,
        "s": ns_sec,
        "m": ns_sec * 60,
        "h": ns_sec * 60 * 60,
        "d": ns_sec * 3_600 * 24,
        "w": ns_sec * 3_600 * 24 * 7,
    }

    # identify duration units and convert to nanoseconds

View on GitHub (pinned to df599052da)

Solutions

  1. Re-copy the repr straight from a real polars DataFrame/Series output so all cells are plain strings
  2. Prefer constructing frames from explicit data (pl.DataFrame({...})) instead of repr parsing in production code
  3. If wrapping from_repr, ensure the input block preserves the default string table shape (headers + quoted/plain string cells)

Example fix

# before
# repr block with a column already in {i64} form pasted into from_repr -> TypeError

# after
df = pl.from_repr(
'''
┌─────┬──────┐
│ a   ┆ b    │
│ --- ┆ ---  │
│ i64 ┆ str  │
├─────┼──────┤
│ 1   ┆ x    │
└─────┴──────┘
''')
Defensive patterns

Strategy: validation

Validate before calling

def repr_block_ok(lines: list[str]) -> bool:
    # all data rows must be parseable as plain strings under the header
    return len(lines) >= 3 and all(line.strip() for line in lines)

Try / catch

try:
    df = pl.from_repr(text)
except TypeError:
    df = None  # fall back to explicit construction from parsed values

Prevention

When it happens

Trigger: Calling pl.from_repr(...) or pl.from_reprs(...) with a hand-edited or already-typed table (e.g. pasted from a session where values were not the default string repr), or where the internal parser produced a non-string column. Not part of the stable public API surface for normal frame construction.

Common situations: Doctests and tutorials that use from_repr; copying a DataFrame repr that was produced with a custom formatting hook; internal code changes in how reprs parse.

Related errors


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