{"record":{"id":"abe14e015e0ad8e2","repo":"pola-rs/polars","slug":"more-new-column-names-are-specified-than-there-are","errorCode":null,"errorMessage":"more new column names are specified than there are selected columns","messagePattern":"more new column names 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":443,"sourceCode":"\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                )\n                raise ValueError(msg)\n\n            # Get column names of requested columns.\n            current_columns = columns[0 : len(new_columns)]\n        elif not has_header:\n            # When there are no header, column names are autogenerated (and known).\n\n            if projection:\n                if columns and len(columns) < len(new_columns):\n                    msg = (\n                        \"more new column names are specified than there are selected\"\n                        \" columns\"\n                    )\n                    raise ValueError(msg)\n                # Convert column indices from projection to 'column_1', 'column_2', ...\n                # column names.\n                current_columns = [\n                    f\"column_{column_idx + 1}\" for column_idx in projection\n                ]","sourceCodeStart":425,"sourceCodeEnd":461,"githubUrl":"https://github.com/pola-rs/polars/blob/df599052daf96e7a9cc30a3b0c6bd25d6947e3c0/py-polars/src/polars/io/csv/functions.py#L425-L461","documentation":"When new_columns is used to rename CSV columns and schema_overrides is a dict (or was converted to one), read_csv must map dtype keys back to the current names (py-polars/src/polars/io/csv/functions.py:437-448). In the explicit-`columns` branch, if len(columns) < len(new_columns) there are more new names than selected columns and ValueError is raised before parsing.","triggerScenarios":"pl.read_csv(f, columns=['a','b'], new_columns=['x','y','z'], schema_overrides={'a': pl.Int64}) - two selected columns but three rename targets.","commonSituations":"Renaming while selecting a subset: the rename list kept entries for columns that were later removed from the selection; evolving schemas where a column was dropped but new_columns was not updated.","solutions":["Make new_columns no longer than columns (drop entries for unselected columns)","Rename after reading with df.rename({'a': 'x'}) instead of new_columns","Derive columns, new_columns, and schema_overrides from one validated config object"],"exampleFix":"# before\ndf = pl.read_csv(\"f.csv\", columns=[\"a\", \"b\"], new_columns=[\"x\", \"y\", \"z\"], schema_overrides={\"a\": pl.Int64})\n# after\ndf = pl.read_csv(\"f.csv\", columns=[\"a\", \"b\"], new_columns=[\"x\", \"y\"], schema_overrides={\"a\": pl.Int64})","handlingStrategy":"validation","validationCode":"def check_new_columns(columns, new_columns, schema_overrides) -> None:\n    if new_columns and schema_overrides is not None and columns is not None:\n        if len(columns) < len(new_columns):\n            raise ValueError(\n                f\"{len(new_columns)} new names for {len(columns)} selected columns\"\n            )","typeGuard":null,"tryCatchPattern":"try:\n    df = pl.read_csv(path, columns=cols, new_columns=new, schema_overrides=ov)\nexcept ValueError as e:\n    if \"more new column names\" in str(e):\n        df = pl.read_csv(path, columns=cols, schema_overrides=ov).rename(\n            dict(zip(cols, new))\n        )\n    else:\n        raise","preventionTips":["Keep new_columns exactly as long as the selection; rename after reading for anything else","Treat columns/new_columns/schema_overrides as one config unit that changes together","Prefer df.rename() post-read for partial or conditional renames"],"tags":["polars","csv","rename","columns","valueerror"],"backgroundTag":null,"analyzedSha":"df599052daf96e7a9cc30a3b0c6bd25d6947e3c0","analyzedAt":"2026-08-16T12:10:03.978Z","schemaVersion":2},"datasetVersion":"2026-08-16T13:17:31.715Z"}