pola-rs/polars · error · TypeError

the `columns` argument should contain a list of all…

Error message

the `columns` argument should contain a list of all integers or all string values

What it means

`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.

Solutions

  1. Normalize the list to all strings (names) or all ints (indices) before the call
  2. Convert numeric strings to ints if you intend projections: `[int(c) for c in columns]`
  3. Validate homogeneity: `all(isinstance(c, str) for c in columns) or all(isinstance(c, int) for c in columns)`

Example fix

// before
read_csv(f, columns=['a', 1])
// after
read_csv(f, columns=['a', 'b'])  # or columns=[0, 1]
Defensive patterns

Strategy: validation

Validate before calling

if columns is not None:
    if not (all(isinstance(c, str) for c in columns) or all(isinstance(c, int) for c in columns)):
        raise TypeError('columns must be all strings or all ints')

Type guard

def valid_columns(cols) -> bool:
    return cols is None or all(isinstance(c, str) for c in cols) or all(isinstance(c, int) for c in cols)

Try / catch

try:
    df = pl.read_csv(f, columns=columns)
except TypeError as e:
    if 'all integers or all string values' in str(e):
        columns = [int(c) for c in columns]
        df = pl.read_csv(f, columns=columns)
    else:
        raise

Prevention

When it happens

Trigger: `read_csv(..., columns=[0, 'a'])`, `columns=[True]`, or a list of floats/other types; commonly when columns come from unparsed user input.

Common situations: Config files where indices were read as strings; mixing names and indices assuming polars supports both; passing numpy mixed arrays.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


AI-assisted analysis of pola-rs/polars@fe841f959e (2026-09-18). Data as JSON: /api/errors/49d5dc31ff2729e7. Report an issue: GitHub.

Appendix: source

Thrown at py-polars/src/polars/io/_utils.py:76

    if columns is None:
        return None, None

    projection: Sequence[int] | None = None
    column_names: Sequence[str] | None = None

    if isinstance(columns, str):
        column_names = [columns]
    elif isinstance(columns, int):
        projection = [columns]
    elif is_str_sequence(columns):
        _ensure_columns_are_unique(columns)
        column_names = columns
    elif is_int_sequence(columns):
        _ensure_columns_are_unique(columns)
        projection = columns
    else:
        msg = "the `columns` argument should contain a list of all integers or all string values"
        raise TypeError(msg)

    return projection, column_names


def _ensure_columns_are_unique(columns: Sequence[str] | Sequence[int]) -> None:
    if len(columns) != len(set(columns)):
        msg = f"`columns` arg should only have unique values, got {columns!r}"
        raise ValueError(msg)


def parse_row_index_args(
    row_index_name: str | None = None,
    row_index_offset: int = 0,
) -> tuple[str, int] | None:
    """
    Parse the `row_index_name` and `row_index_offset` arguments of an I/O function.

    The Rust functions take a single tuple rather than two separate arguments.

View on GitHub (pinned to fe841f959e)