Repository navigation
GH-53290: [Python] CSV: expose pad_short_rows parse option in Python - #54691
Mr-Nobody-Anonymous wants to merge 6 commits into
Conversation
…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
|
|
…_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)
|
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. |
…ontext in test_csv_format_pad_short_rows
|
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 |
…egacy tuple handling
…mes in test_csv.py
| # 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) |
There was a problem hiding this comment.
This line won't be reached?
| dataset_reader.to_table(dataset_default) |
…_format_pad_short_rows
|
@pearu could you do a quick pass here? |
pearu
left a comment
There was a problem hiding this comment.
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): |
There was a problem hiding this comment.
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:
| 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], | |
| } | |
| # Default format fails on short rows | ||
| with pytest.raises(pa.ArrowInvalid, match="Expected 3 columns, got 2: 4,5"): | ||
| ds.dataset(path, format='csv') |
There was a problem hiding this comment.
Strictly speaking, this is not about the new option nor a regression test, so I'd suggest:
| # Default format fails on short rows | |
| with pytest.raises(pa.ArrowInvalid, match="Expected 3 columns, got 2: 4,5"): | |
| ds.dataset(path, format='csv') |
|
Thanks for the review @pearu ! |

Rationale for this change
In #50926,
pad_short_rowswas 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?
pad_short_rowsonCCSVParseOptionsinpython/pyarrow/includes/libarrow.pxd.pad_short_rowsparameter topyarrow.csv.ParseOptions.__init__, defaulting toNone(preserving C++ defaultfalse).pad_short_rowsproperty (getter and setter) onpyarrow.csv.ParseOptions.ParseOptions.equals(),_repr_base(),__getstate__(), and__setstate__().ParseOptions.pad_short_rowsindocs/source/python/csv.rst.python/pyarrow/tests/test_csv.py:pad_short_rowsdiffers.pad_short_rows=Trueacross serial and parallel readers for numerical and string datasets.pad_short_rows=Falseretains strict error behavior.python/pyarrow/tests/test_dataset.pyfords.CsvFileFormat(pa.csv.ParseOptions(pad_short_rows=True))pickling and dataset reading viads.dataset().Are these changes tested?
Yes. Added tests in
test_csv.pyandtest_dataset.pycovering 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.ParseOptionsnow acceptspad_short_rows(defaulting toFalse), 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:
Reviewed before submission by: