{"record":{"id":"da82a22e0f65b44d","repo":"pola-rs/polars","slug":"data-does-not-match-the-number-of-columns","errorCode":null,"errorMessage":"data does not match the number of columns","messagePattern":"data does not match the number of columns","errorType":"validation","errorClass":"ShapeError","httpStatus":null,"severity":"error","filePath":"py-polars/src/polars/_utils/construction/dataframe.py","lineNumber":226,"sourceCode":"        return columns, schema_overrides\n\n    # determine column names from schema\n    if isinstance(schema, Mapping):\n        column_names: list[str] = list(schema)\n        schema = list(schema.items())\n    else:\n        column_names = []\n        for i, col in enumerate(schema):\n            if isinstance(col, str):\n                unnamed = not col and col not in schema_overrides\n                col = f\"column_{i}\" if unnamed else col\n            else:\n                col = col[0]\n            column_names.append(col)\n\n    if n_expected is not None and len(column_names) != n_expected:\n        msg = \"data does not match the number of columns\"\n        raise ShapeError(msg)\n\n    # determine column dtypes from schema and lookup_names\n    lookup: dict[str, str] | None = (\n        {\n            col: name\n            for col, name in zip_longest(column_names, lookup_names)\n            if name is not None\n        }\n        if lookup_names\n        else None\n    )\n\n    column_dtypes: dict[str, PolarsDataType] = {}\n    for col in schema:\n        if isinstance(col, str):\n            continue\n\n        name, dtype = col","sourceCodeStart":208,"sourceCodeEnd":244,"githubUrl":"https://github.com/pola-rs/polars/blob/fe841f959ef4d2ceefc05a310d33ed7b1ab24e5e/py-polars/src/polars/_utils/construction/dataframe.py#L208-L244","documentation":"Raised by _unpack_schema (as a ShapeError) when explicit column names are provided but their count differs from the number of columns the data actually has. Polars uses n_expected as the arity contract from the input data and fails fast when the supplied names don't line up.","triggerScenarios":"pl.DataFrame([[1,2],[3,4]], schema=['a','b','c']) — three names for two columns; sequence-of-sequences, sequence-of-series, or sequence-of-dicts input with a schema list whose length differs from the data width.","commonSituations":"Adding a column to the data without updating the schema list, copying a constructor call from another snippet and forgetting to adjust column names, row- vs column-oriented confusion about expected width.","solutions":["Make the number of column names equal the number of data columns","Drop the schema/column_names argument to let Polars name columns column_0, column_1, ...","Verify data width with len(data[0]) for row data or len(data) for column data before constructing"],"exampleFix":"// before\npl.DataFrame([[1, 2], [3, 4]], schema=['a', 'b', 'c'])\n// after\npl.DataFrame([[1, 2], [3, 4]], schema=['a', 'b'])","handlingStrategy":"validation","validationCode":"names = schema if isinstance(schema, list) else list(schema or {})\nassert len(names) == len(data[0]), f'{len(names)} names vs {len(data[0])} columns'","typeGuard":null,"tryCatchPattern":"try:\n    df = pl.DataFrame(rows, schema=names)\nexcept pl.exceptions.ShapeError as e:\n    if 'does not match the number of columns' in str(e):\n        df = pl.DataFrame(rows)\n    else:\n        raise","preventionTips":["Build column-name lists from the data shape, not by hand","Add an assertion comparing len(schema) to data width before constructing","When width changes, search call sites that hardcode schema"],"tags":["python","polars","dataframe-construction","shape"],"backgroundTag":"shape-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"}