{"record":{"id":"1aaa2dee1a9ce3e5","repo":"pola-rs/polars","slug":"dataframe-should-contain-only-string-repr-data-found-tp-r","errorCode":null,"errorMessage":"DataFrame should contain only String repr data; found {tp!r}","messagePattern":"DataFrame should contain only String repr data; found (.+?)","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"py-polars/src/polars/_utils/various.py","lineNumber":337,"sourceCode":"\n    Parameters\n    ----------\n    df\n        Dataframe containing string-repr column data.\n    schema\n        DataFrame schema containing the desired end-state types.\n\n    Notes\n    -----\n    Table repr strings are less strict (or different) than equivalent CSV data, so need\n    special handling; as this function is only used for reprs, parsing is flexible.\n    \"\"\"\n    tp: PolarsDataType | None\n    if not df.is_empty():\n        for tp in df.schema.values():\n            if tp != String:\n                msg = f\"DataFrame should contain only String repr data; found {tp!r}\"\n                raise TypeError(msg)\n\n    special_floats = {\"-inf\", \"+inf\", \"inf\", \"nan\"}\n\n    # duration string scaling\n    ns_sec = 1_000_000_000\n    duration_scaling = {\n        \"ns\": 1,\n        \"us\": 1_000,\n        \"µs\": 1_000,\n        \"ms\": 1_000_000,\n        \"s\": ns_sec,\n        \"m\": ns_sec * 60,\n        \"h\": ns_sec * 60 * 60,\n        \"d\": ns_sec * 3_600 * 24,\n        \"w\": ns_sec * 3_600 * 24 * 7,\n    }\n\n    # identify duration units and convert to nanoseconds","sourceCodeStart":319,"sourceCodeEnd":355,"githubUrl":"https://github.com/pola-rs/polars/blob/fe841f959ef4d2ceefc05a310d33ed7b1ab24e5e/py-polars/src/polars/_utils/various.py#L319-L355","documentation":"_cast_repr_strings_with_schema converts a DataFrame built from polars object-repr text back into typed values, and it requires every column to be String typed. If any column has a non-String dtype, it raises TypeError naming the offending dtype. It's an internal precondition on repr-parsing input.","triggerScenarios":"Constructing a DataFrame for _from_dataframe_repr/_from_series_repr parsing where a column was created with a non-String dtype (e.g. from_dict with ints/None-inferring dtypes), then passing it to the repr parser.","commonSituations":"Building repr DataFrames manually in tests or tooling and letting polars infer dtypes (numbers, nulls), instead of forcing pl.String on all columns.","solutions":["Cast the DataFrame to all-String before parsing: df.cast({c: pl.String for c in df.columns}).","Build the DataFrame with an explicit schema of pl.String for every column.","Ensure stringified values: convert each value with str(v) rather than relying on inference."],"exampleFix":"// before\ndf = pl.DataFrame({'a': [1, 2]})  # Int64 columns\n// after\ndf = pl.DataFrame({'a': ['1', '2']}, schema={'a': pl.String})","handlingStrategy":"validation","validationCode":"if df.schema and not all(tp == pl.String for tp in df.schema.values()):\n    df = df.cast({c: pl.String for c in df.columns})","typeGuard":"def is_all_string(df) -> bool:\n    return all(tp == pl.String for tp in df.schema.values())","tryCatchPattern":"try:\n    out = _cast_repr_strings_with_schema(df, schema)\nexcept TypeError as e:\n    if 'only String repr data' in str(e):\n        out = _cast_repr_strings_with_schema(df.cast({c: pl.String for c in df.columns}), schema)","preventionTips":["Build repr DataFrames with explicit pl.String schema","str() every value before putting it in the repr DataFrame","Never rely on dtype inference for repr input"],"tags":["python","polars","dtype","repr"],"backgroundTag":"dtype-mismatch","analyzedSha":"fe841f959ef4d2ceefc05a310d33ed7b1ab24e5e","analyzedAt":"2026-09-18T22:14:11.667Z","contentChangedAt":"2026-09-18T22:14:11.667Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}