Skip to content

Number the index across batches in to_csv and to_sql - #8834

Open
mertkayacs wants to merge 3 commits into
huggingface:mainfrom
mertkayacs:fix-writer-index-across-batches
Open

mertkayacs wants to merge 3 commits into
huggingface:mainfrom
mertkayacs:fix-writer-index-across-batches

Conversation

@mertkayacs

Copy link
Copy Markdown

to_csv(index=True) and to_sql(index=True) wrote an index column that restarts at 0 in every batch: CsvDatasetWriter._batch_csv and SqlDatasetWriter._batch_sql convert each batch with batch.to_pandas(), which gives the batch a fresh RangeIndex from 0, and pandas writes that. With the default batch_size the column reads 0 … 9999, 0 … 9999, … for anything larger than one batch, and the docstrings of both methods present index=True plus index_label as the way to write the row index. The batch frame now gets RangeIndex(offset, offset + len(df)) before pandas writes it, so the column numbers the dataset's rows under any batch_size and num_proc.

datasets never restores a pandas index from a batch (from_pandas(preserve_index=True) keeps __index_level_0__ as an ordinary column, and query_table(...).to_pandas() always returns a RangeIndex from 0), so the offset is the right origin for every batch. The renumbering only runs when index is set, and the default index=False path is unchanged.

I left to_json alone: with orient="records" pandas writes no index, and the orients that would write one (split, table) already refuse batch_size < num_rows.

Verification

$ python -m pytest tests/io/test_csv.py tests/io/test_sql.py -q   # Python 3.12, pyarrow 26.0.0, pandas 3.0.6
46 passed, 6 warnings in 1.71s
$ ruff check src/datasets/io/csv.py src/datasets/io/sql.py tests/io/test_csv.py tests/io/test_sql.py && ruff format --check src/datasets/io/csv.py src/datasets/io/sql.py tests/io/test_csv.py tests/io/test_sql.py
All checks passed!
4 files already formatted
$ python -m pytest tests/test_arrow_dataset.py -q -k "to_csv or to_sql"
4 passed, 4 warnings in 3.15s
# not run: the rest of tests/

With src/datasets/io/csv.py and src/datasets/io/sql.py back at 18378f7 and the tests kept, the three new cases (test_dataset_to_csv_index_numbers_rows_across_batches[1|2], test_dataset_to_sql_index_numbers_rows_across_batches) fail with At index 3 diff: '0' != '3' and (0, 3) != (3, 3).

The SQL case runs with one process on purpose: to_sql with num_proc > 1 and more than one batch fails on main with table already exists whenever a later batch reaches the database before the first one, which is a separate defect I am reporting on its own.

Fixes #8831

@charan-rathore

Copy link
Copy Markdown

The RangeIndex renumbering is the right fix and covers both writers. One case worth a pin: a dataset that went through select() carries _indices, and offset is then a position inside the sliced view, so the written index numbers the slice 0..len rather than the original rows. That matches the natural "row number of what I exported" reading, but it is a choice someone will eventually ask about - a one-line test with ds.select(range(5, 12)) would make it deliberate either way.

@mertkayacs

Copy link
Copy Markdown
Author

Good catch on the select() case. The intent is that the index numbers the exported rows, so a sliced view gets 0..len of the slice rather than the original positions. That is the reading you described, the row number of what you exported. I'll add the ds.select(range(5, 12)) test so it stays deliberate either way.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

to_csv and to_sql with index=True restart the index at 0 in every batch

2 participants