{"record":{"id":"49d5dc31ff2729e7","repo":"pola-rs/polars","slug":"the-columns-argument-should-contain-a-list-of-al","errorCode":null,"errorMessage":"the `columns` argument should contain a list of all integers or all string values","messagePattern":"the `columns` argument should contain a list of all integers or all string values","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"py-polars/src/polars/io/_utils.py","lineNumber":76,"sourceCode":"    if columns is None:\n        return None, None\n\n    projection: Sequence[int] | None = None\n    column_names: Sequence[str] | None = None\n\n    if isinstance(columns, str):\n        column_names = [columns]\n    elif isinstance(columns, int):\n        projection = [columns]\n    elif is_str_sequence(columns):\n        _ensure_columns_are_unique(columns)\n        column_names = columns\n    elif is_int_sequence(columns):\n        _ensure_columns_are_unique(columns)\n        projection = columns\n    else:\n        msg = \"the `columns` argument should contain a list of all integers or all string values\"\n        raise TypeError(msg)\n\n    return projection, column_names\n\n\ndef _ensure_columns_are_unique(columns: Sequence[str] | Sequence[int]) -> None:\n    if len(columns) != len(set(columns)):\n        msg = f\"`columns` arg should only have unique values, got {columns!r}\"\n        raise ValueError(msg)\n\n\ndef parse_row_index_args(\n    row_index_name: str | None = None,\n    row_index_offset: int = 0,\n) -> tuple[str, int] | None:\n    \"\"\"\n    Parse the `row_index_name` and `row_index_offset` arguments of an I/O function.\n\n    The Rust functions take a single tuple rather than two separate arguments.","sourceCodeStart":58,"sourceCodeEnd":94,"githubUrl":"https://github.com/pola-rs/polars/blob/fe841f959ef4d2ceefc05a310d33ed7b1ab24e5e/py-polars/src/polars/io/_utils.py#L58-L94","documentation":"`parse_columns_arg` accepts `columns` either as a list of column-name strings or a list of integer projection indices. A mixed or otherwise non-conforming list (neither all ints nor all strings) raises TypeError.","triggerScenarios":"`read_csv(..., columns=[0, 'a'])`, `columns=[True]`, or a list of floats/other types; commonly when columns come from unparsed user input.","commonSituations":"Config files where indices were read as strings; mixing names and indices assuming polars supports both; passing numpy mixed arrays.","solutions":["Normalize the list to all strings (names) or all ints (indices) before the call","Convert numeric strings to ints if you intend projections: `[int(c) for c in columns]`","Validate homogeneity: `all(isinstance(c, str) for c in columns) or all(isinstance(c, int) for c in columns)`"],"exampleFix":"// before\nread_csv(f, columns=['a', 1])\n// after\nread_csv(f, columns=['a', 'b'])  # or columns=[0, 1]","handlingStrategy":"validation","validationCode":"if columns is not None:\n    if not (all(isinstance(c, str) for c in columns) or all(isinstance(c, int) for c in columns)):\n        raise TypeError('columns must be all strings or all ints')","typeGuard":"def valid_columns(cols) -> bool:\n    return cols is None or all(isinstance(c, str) for c in cols) or all(isinstance(c, int) for c in cols)","tryCatchPattern":"try:\n    df = pl.read_csv(f, columns=columns)\nexcept TypeError as e:\n    if 'all integers or all string values' in str(e):\n        columns = [int(c) for c in columns]\n        df = pl.read_csv(f, columns=columns)\n    else:\n        raise","preventionTips":["Normalize column selections to strings (names) at ingestion points","Never mix names and indices in one list"],"tags":["python","polars","io","type-error"],"backgroundTag":"invalid-argument-value","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"}