{"record":{"id":"f530b0847d5618dd","repo":"pola-rs/polars","slug":"more-dtypes-overrides-are-specified-than-there-are","errorCode":null,"errorMessage":"more dtypes overrides are specified than there are selected columns","messagePattern":"more dtypes overrides are specified than there are selected columns","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"py-polars/src/polars/io/csv/functions.py","lineNumber":424,"sourceCode":"    if projection and schema_overrides and isinstance(schema_overrides, list):\n        if len(projection) < len(schema_overrides):\n            msg = \"more schema overrides are specified than there are selected columns\"\n            raise ValueError(msg)\n\n        # Fix list of dtypes when used together with projection as polars CSV reader\n        # wants a list of dtypes for the x first columns before it does the projection.\n        dtypes_list: list[PolarsDataType] = [String] * (max(projection) + 1)\n\n        for idx, column_idx in enumerate(projection):\n            if idx < len(schema_overrides):\n                dtypes_list[column_idx] = schema_overrides[idx]\n\n        schema_overrides = dtypes_list\n\n    if columns and schema_overrides and isinstance(schema_overrides, list):\n        if len(columns) < len(schema_overrides):\n            msg = \"more dtypes overrides are specified than there are selected columns\"\n            raise ValueError(msg)\n\n        # Map list of dtypes when used together with selected columns as a dtypes dict\n        # so the dtypes are applied to the correct column instead of the first x\n        # columns.\n        schema_overrides = dict(zip(columns, schema_overrides, strict=False))\n\n    if new_columns and schema_overrides and isinstance(schema_overrides, dict):\n        current_columns = None\n\n        # As new column names are not available yet while parsing the CSV file, rename\n        # column names in dtypes to old names (if possible) so they can be used during\n        # CSV parsing.\n        if columns:\n            if len(columns) < len(new_columns):\n                msg = (\n                    \"more new column names are specified than there are selected\"\n                    \" columns\"\n                )","sourceCodeStart":406,"sourceCodeEnd":442,"githubUrl":"https://github.com/pola-rs/polars/blob/df599052daf96e7a9cc30a3b0c6bd25d6947e3c0/py-polars/src/polars/io/csv/functions.py#L406-L442","documentation":"The sibling check for string columns (py-polars/src/polars/io/csv/functions.py:423-433): when columns=['a','b'] and schema_overrides is a list, polars zips them into a name-to-dtype dict. If len(columns) < len(schema_overrides), the zip would silently drop the extra dtypes, so ValueError ('more dtypes overrides are specified than there are selected columns') is raised instead.","triggerScenarios":"pl.read_csv(f, columns=['a','b'], schema_overrides=[pl.Int64, pl.Utf8, pl.Date]) - two names, three dtypes.","commonSituations":"Dtype lists inherited from a wider schema than the currently selected columns; merging overrides from multiple configs without re-trimming to the selection.","solutions":["Match lengths: exactly one dtype per selected column","Use a dict {'a': pl.Int64} for sparse, name-keyed overrides","Generate columns and schema_overrides from the same single config source"],"exampleFix":"# before\ndf = pl.read_csv(\"f.csv\", columns=[\"a\", \"b\"], schema_overrides=[pl.Int64, pl.Utf8, pl.Date])\n# after\ndf = pl.read_csv(\"f.csv\", columns=[\"a\", \"b\"], schema_overrides={\"a\": pl.Int64})","handlingStrategy":"validation","validationCode":"def check_columns_overrides(columns, schema_overrides) -> None:\n    if (\n        isinstance(columns, list)\n        and isinstance(schema_overrides, list)\n        and len(columns) < len(schema_overrides)\n    ):\n        raise ValueError(\n            f\"{len(schema_overrides)} dtypes for {len(columns)} named columns; trim or use a dict\"\n        )","typeGuard":null,"tryCatchPattern":"try:\n    df = pl.read_csv(path, columns=cols, schema_overrides=ov)\nexcept ValueError as e:\n    if \"more dtypes overrides\" in str(e) and isinstance(ov, list):\n        df = pl.read_csv(path, columns=cols, schema_overrides=dict(zip(cols, ov)))\n    else:\n        raise","preventionTips":["Use a name-keyed dict when overriding only some columns","Regenerate dtype lists whenever the column selection changes","Assert len(columns) >= len(schema_overrides) in config validation"],"tags":["polars","csv","columns","dtypes","valueerror"],"backgroundTag":null,"analyzedSha":"df599052daf96e7a9cc30a3b0c6bd25d6947e3c0","analyzedAt":"2026-08-16T12:10:03.978Z","schemaVersion":2},"datasetVersion":"2026-08-16T13:17:31.715Z"}