arrow icon indicating copy to clipboard operation
arrow copied to clipboard

[C++][Python] Duplicate csv header when table batches start with empty

Open 0x26res opened this issue 3 years ago • 5 comments

Describe the bug, including details regarding any error messages, version, and platform.

pyarrow.csv.write_csv works as expected, but when the first record batch in a table is empty, it writes two headers (on 2 different lines) instead of one:

import pyarrow.csv
import pyarrow as pa
import io

table = pa.table({"col1": ["a", "b", "c"]})

with io.BytesIO() as fp:
    pyarrow.csv.write_csv(table, fp)
    fp.seek(0)
    assert fp.read() == b'"col1"\n"a"\n"b"\n"c"\n'

with io.BytesIO() as fp:
    pyarrow.csv.write_csv(pa.concat_tables([table.schema.empty_table(), table]), fp)
    fp.seek(0)
    # THIS IS WRONG:
    assert fp.read() == b'"col1"\n"col1"\n"a"\n"b"\n"c"\n'

Component(s)

Python

0x26res avatar Jul 26 '23 11:07 0x26res

Hello, if this still needs to be fixed I could take it on. I'm looking for a first issue to contribute to, but I'm not sure about this one since it's been around for a while. Let me know!

theroshogolla avatar Nov 19 '24 11:11 theroshogolla

Has this error been resolved?

harrydevforlife avatar Mar 20 '25 08:03 harrydevforlife

I have been tested, and I received inconsistent results whether duplicated or not, even though the write options include header=True or header=False. Therefore, I decided to use Python's native CSV library for safety: https://docs.python.org/3/library/csv.html.

harrydevforlife avatar Mar 21 '25 03:03 harrydevforlife

I can still see this error. It happens when the table is made of multiple chunks and the first chunk in each column is empty.

Workaround is to do .combine_chunks() on the table before writing...

jacek-pliszka avatar Jul 23 '25 11:07 jacek-pliszka

Issue resolved by pull request 48718 https://github.com/apache/arrow/pull/48718

wgtmac avatar Jan 27 '26 15:01 wgtmac