{"record":{"id":"5742f47d46fb964b","repo":"pola-rs/polars","slug":"input-string-does-not-contain-dataframe-or-series","errorCode":null,"errorMessage":"input string does not contain DataFrame or Series","messagePattern":"input string does not contain DataFrame or Series","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"py-polars/src/polars/convert/general.py","lineNumber":896,"sourceCode":"    ... )\n    >>> s.to_list()\n    [True, False, True]\n    \"\"\"\n    # find DataFrame table...\n    if (tbl_repr_type := _extract_table(data)) is not None:\n        return _from_dataframe_repr(*tbl_repr_type)\n\n    # ...or Series in the given string\n    m = re.search(\n        pattern=r\"(?:shape: (\\(\\d+,\\))\\n.*?)?Series:\\s+([^\\n]+)\\s+\\[([^\\n]+)](.*)\",\n        string=data,\n        flags=re.DOTALL,\n    )\n    if m is not None:\n        return _from_series_repr(m)\n\n    msg = \"input string does not contain DataFrame or Series\"\n    raise ValueError(msg)\n\n\ndef _from_dataframe_repr(tbl: str, table_repr: TableRepr) -> DataFrame:\n    \"\"\"Reconstruct a DataFrame from a table repr string.\"\"\"\n    from polars.datatypes.convert import dtype_short_repr_to_dtype\n    from polars.io.database._inference import dtype_from_database_typename\n\n    def _dtype_from_name(tp: str | None) -> PolarsDataType | None:\n        return (\n            None\n            if tp is None\n            else (\n                dtype_short_repr_to_dtype(tp)\n                or dtype_from_database_typename(tp, raise_unmatched=False)\n            )\n        )\n\n    # associated regex patterns for the given table format","sourceCodeStart":878,"sourceCodeEnd":914,"githubUrl":"https://github.com/pola-rs/polars/blob/df599052daf96e7a9cc30a3b0c6bd25d6947e3c0/py-polars/src/polars/convert/general.py#L878-L914","documentation":"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.","triggerScenarios":"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.","commonSituations":"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).","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)"],"exampleFix":"# before\npl.from_repr(\"shape: (2, 2)\\\\n┌─────┐\\\\n│ a ─┐\\\\n...\")  # double-escaped literal\n\n# after\nimport json\npl.from_repr(json.loads('\"shape: (2, 2)\\\\n┌─────┐\\\\n...\"'))  # unescaped once, real newlines","handlingStrategy":"validation","validationCode":"import re\n\ndef looks_like_polars_repr(s: str) -> bool:\n    return bool(\n        re.search(r'shape: \\(\\d+,? ?\\d*\\)\\n', s)\n        or re.search(r'Series:\\s+[^\\n]+\\s+\\[[^\\n]+]', s)\n    )\n\nif not looks_like_polars_repr(text):\n    raise ValueError('not a polars DataFrame/Series repr; refusing to call from_repr')","typeGuard":null,"tryCatchPattern":"try:\n    obj = pl.from_repr(text)\nexcept ValueError as e:\n    if 'does not contain DataFrame or Series' in str(e):\n        # recover: try real serialization or surface a clear fixture error\n        raise AssertionError(f'fixture string is not a polars repr: {text[:80]!r}') from e\n    raise","preventionTips":["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"],"tags":["from-repr","parsing","string-input","serialization"],"backgroundTag":null,"analyzedSha":"df599052daf96e7a9cc30a3b0c6bd25d6947e3c0","analyzedAt":"2026-08-16T12:10:03.978Z","schemaVersion":2},"datasetVersion":"2026-08-16T13:17:31.715Z"}