Skip to content

GH-53290: [Python] CSV: expose pad_short_rows parse option in Python - #54691

Open
Mr-Nobody-Anonymous wants to merge 6 commits into
apache:mainfrom
Mr-Nobody-Anonymous:gh-53290-csv-pad-short-rows
Open

Mr-Nobody-Anonymous wants to merge 6 commits into
apache:mainfrom
Mr-Nobody-Anonymous:gh-53290-csv-pad-short-rows

Conversation

@Mr-Nobody-Anonymous

@Mr-Nobody-Anonymous Mr-Nobody-Anonymous commented Oct 20, 2026 •

Copy link
Copy Markdown

Rationale for this change

In #50926, pad_short_rows was implemented in the C++ CSV parser/reader (arrow::csv::ParseOptions::pad_short_rows), allowing rows with fewer fields than expected to be padded with null values rather than failing. However, this option was not yet exposed to the Python bindings (#53290).

What changes are included in this PR?

  • Expose pad_short_rows on CCSVParseOptions in python/pyarrow/includes/libarrow.pxd.
  • Add pad_short_rows parameter to pyarrow.csv.ParseOptions.__init__, defaulting to None (preserving C++ default false).
  • Add pad_short_rows property (getter and setter) on pyarrow.csv.ParseOptions.
  • Update ParseOptions.equals(), _repr_base(), __getstate__(), and __setstate__().
  • Document ParseOptions.pad_short_rows in docs/source/python/csv.rst.
  • Add unit tests in python/pyarrow/tests/test_csv.py:
    • Options validation, string/repr output, and serialization/pickling.
    • Equality differentiation when only pad_short_rows differs.
    • CSV table and streaming reading with pad_short_rows=True across serial and parallel readers for numerical and string datasets.
    • Distinction between missing trailing fields (padded as nulls) and explicitly empty fields (preserved as empty strings).
    • Verification that excess columns still raise an error.
    • Header-only CSV input verification.
    • Verification that default pad_short_rows=False retains strict error behavior.
    • Reading with explicit column names (including when the first row is short) and typed schema conversions.
  • Add test coverage in python/pyarrow/tests/test_dataset.py for ds.CsvFileFormat(pa.csv.ParseOptions(pad_short_rows=True)) pickling and dataset reading via ds.dataset().

Are these changes tested?

Yes. Added tests in test_csv.py and test_dataset.py covering parse options validation, pickling, repr, and reading short rows across serial, parallel, streaming, and dataset readers.

Are there any user-facing changes?

Yes. pyarrow.csv.ParseOptions now accepts pad_short_rows (defaulting to False), allowing users to opt into padding short rows with nulls.

Was AI used for this PR?

AI assistance was used for code drafting, technical audit, and test authoring. The architecture, C++ mapping, test cases, and documentation were reviewed and verified.

PR code and description written by:

  • Human
  • AI

Reviewed before submission by:

  • Human
  • AI
  • Not reviewed

…ython

Add pad_short_rows to pyarrow.csv.ParseOptions, allowing short CSV rows
with fewer fields than expected to be padded with null values rather
than raising an ArrowInvalid parse error.

* Add pad_short_rows property, init argument, equals, repr, and serialization in _csv.pyx
* Expose pad_short_rows in libarrow.pxd CCSVParseOptions
* Add tests in test_csv.py and test_dataset.py
* Document option in docs/source/python/csv.rst
Copilot AI balanced review requested due to automatic review settings October 20, 2026 10:52

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@github-actions

Copy link
Copy Markdown

⚠️ GitHub issue #53290 has been automatically assigned in GitHub to PR creator.

…_rows test coverage

* Correct integer type expectation for numerical short row CSV tests
* Add string data test cases for pad_short_rows
* Test distinction between missing trailing fields and empty fields
* Test error on excess columns when pad_short_rows=True
* Test header-only input with pad_short_rows=True
* Test backward-compatible unpickling of legacy 7-element ParseOptions state
* Test ParseOptions equality differentiation on pad_short_rows
* Add dataset reader integration test with CsvFileFormat(pad_short_rows=True)
Copilot AI balanced review requested due to automatic review settings October 20, 2026 11:35

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The new dataset test raises during schema inference outside its exception assertion, preventing both fixture variants from reaching the padding checks.

1 open issue

Review effort: Balanced

Comment thread python/pyarrow/tests/test_dataset.py Resolved
@HuaHuaY

HuaHuaY commented Nov 2, 2026

Copy link
Copy Markdown
Contributor

There are some invisible characters in the PR description. Could you please fix them? Also, before submitting a PR, you should take the time to read https://arrow.apache.org/docs/dev/developers/overview.html#ai-generated-code.

Copilot AI balanced review requested due to automatic review settings November 19, 2026 12:10

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@Mr-Nobody-Anonymous

Mr-Nobody-Anonymous commented Nov 19, 2026 •

Copy link
Copy Markdown
Author

Thank you for the review! I have cleaned up the PR description to remove all unescaped control characters, reviewed the AI generation guidelines in the developer documentation, and fixed the failing test assertion in test_dataset.py by placing dataset construction within the pytest.raises block.

Comment thread python/pyarrow/_csv.pyx Resolved
Copilot AI balanced review requested due to automatic review settings November 26, 2026 14:32

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

Copilot AI balanced review requested due to automatic review settings November 26, 2026 15:04

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@Mr-Nobody-Anonymous

Copy link
Copy Markdown
Author

Hi @pitrou and @HuaHuaY, all CI checks have passed on the updated branch (commit 177c040, with simplified ParseOptions.setstate and the short-first-row test case). Whenever you have a moment, could you please take a look for final review and merge? Thanks!

Comment thread python/pyarrow/tests/test_dataset.py Outdated
# Default format fails on short rows
with pytest.raises(pa.ArrowInvalid, match="Expected 3 columns, got 2: 4,5"):
dataset_default = ds.dataset(path, format='csv')
dataset_reader.to_table(dataset_default)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line won't be reached?

Suggested change
dataset_reader.to_table(dataset_default)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch @rok, thanks! Removed the unreachable to_table call and unused assignment in commit 5426a74.

@rok

rok commented Dec 3, 2026

Copy link
Copy Markdown
Member

@pearu could you do a quick pass here?

@pearu pearu left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The binding change looks good to me. I'd ask for the tests to be trimmed to the plumbing, since most of the new cases re-test C++ behaviour that already has C++ tests.

Once the tests are trimmed, the test list under "What changes are included" and "Are these changes tested?" needs updating to match.

'b': ["e", "j"],
}

def test_pad_short_rows(self):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think Python tests only need to show that the option reaches the C++ reader. Most cases in test_pad_short_rows are already covered in C++, so one read with padding is enough:

Suggested change
def test_pad_short_rows(self):
def test_pad_short_rows(self):
rows = b"1,2\n3,4,5,6\n"
table = self.read_bytes(
rows,
read_options=ReadOptions(column_names=["w", "x", "y", "z"]),
parse_options=ParseOptions(pad_short_rows=True),
)
assert table.to_pydict() == {
"w": [1, 3],
"x": [2, 4],
"y": [None, 5],
"z": [None, 6],
}

Comment on lines +3576 to +3578
# Default format fails on short rows
with pytest.raises(pa.ArrowInvalid, match="Expected 3 columns, got 2: 4,5"):
ds.dataset(path, format='csv')

@pearu pearu Dec 4, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Strictly speaking, this is not about the new option nor a regression test, so I'd suggest:

Suggested change
# Default format fails on short rows
with pytest.raises(pa.ArrowInvalid, match="Expected 3 columns, got 2: 4,5"):
ds.dataset(path, format='csv')

@rok

rok commented Dec 4, 2026

Copy link
Copy Markdown
Member

Thanks for the review @pearu !
@Mr-Nobody-Anonymous could you please implement requested changes?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

7 participants