Skip to content

[C++][Python] CSV: option to pad short rows with nulls instead of failing #53290

Open

Description

@djouallah

Describe the enhancement requested

Add an option to the CSV reader to read rows that have fewer fields than the number of columns, and fill the missing fields with null. pandas' C engine does this when names is given, and DuckDB has read_csv(..., null_padding = true).

Today the width is fixed by column_names, or by the first row. A shorter row is an error, and invalid_row_handler can only skip it or stop. It cannot keep the row.

import io
import pyarrow as pa
from pyarrow import csv

data = b"a,b\n1,2,3,4\n5,6,7,8\n"
names = ["w", "x", "y", "z"]

csv.read_csv(io.BytesIO(data), read_options=csv.ReadOptions(column_names=names))
# ArrowInvalid: CSV parse error: Expected 4 columns, got 2: a,b

csv.read_csv(io.BytesIO(data), read_options=csv.ReadOptions(autogenerate_column_names=True))
# ArrowInvalid: CSV parse error: Expected 2 columns, got 4: 1,2,3,4

csv.read_csv(io.BytesIO(data),
             read_options=csv.ReadOptions(column_names=names),
             parse_options=csv.ParseOptions(invalid_row_handler=lambda r: "skip"))
# {'w': [1, 5], 'x': [2, 6], 'y': [3, 7], 'z': [4, 8]}  -- the short row is dropped, not padded

Expected, with the new option:

{'w': ['a', '1', '5'], 'x': ['b', '2', '6'], 'y': [None, '3', '7'], 'z': [None, '4', '8']}

Real files have this shape. For example, the AEMO (Australian electricity market) daily CSV files mix record types: the first row is a short header record, and the data rows are wider. pandas with engine="pyarrow" cannot read them, because pandas uses Arrow's reader and Arrow takes the width from the first row. pandas marked this as an Arrow limitation: pandas-dev/pandas#71034

The same request was #27832, closed as stale in January 2026. #28421 added invalid_row_handler, but it can only skip or error.

Tested with pyarrow 26.0.0, Python 3.12, Linux.

Component(s)

C++, Python

Activity

  1. aryan9948 commented on Oct 12, 2026

    @aryan9948

    Hi, I'd like to contribute a fix for this issue. I'm investigating how the CSV reader determines the column count and handles rows with fewer fields than expected. I'd be happy to work on the C++ implementation and Python API if no one is already working on it. Would it be okay for me to take this issue?

  2. HuaHuaY commented on Oct 13, 2026

    @HuaHuaY
    Contributor

    I believe the C++ part has already been implemented in #50926, but this option has not yet been exposed in Python.

  3. Mr-Nobody-Anonymous commented on Oct 20, 2026

    @Mr-Nobody-Anonymous

    I have opened PR #54691 exposing pad_short_rows in pyarrow.csv.ParseOptions along with regression tests and docs.

  4. added a commit that references this issue on Oct 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions