You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Number the index across batches in to_csv and to_sql - #8834
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.
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.
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
to_csv(index=True)andto_sql(index=True)wrote an index column that restarts at 0 in every batch:CsvDatasetWriter._batch_csvandSqlDatasetWriter._batch_sqlconvert each batch withbatch.to_pandas(), which gives the batch a freshRangeIndexfrom 0, and pandas writes that. With the defaultbatch_sizethe column reads 0 … 9999, 0 … 9999, … for anything larger than one batch, and the docstrings of both methods presentindex=Trueplusindex_labelas the way to write the row index. The batch frame now getsRangeIndex(offset, offset + len(df))before pandas writes it, so the column numbers the dataset's rows under anybatch_sizeandnum_proc.datasetsnever restores a pandas index from a batch (from_pandas(preserve_index=True)keeps__index_level_0__as an ordinary column, andquery_table(...).to_pandas()always returns aRangeIndexfrom 0), so the offset is the right origin for every batch. The renumbering only runs whenindexis set, and the defaultindex=Falsepath is unchanged.I left
to_jsonalone: withorient="records"pandas writes no index, and the orients that would write one (split,table) already refusebatch_size < num_rows.Verification
With
src/datasets/io/csv.pyandsrc/datasets/io/sql.pyback 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 withAt index 3 diff: '0' != '3'and(0, 3) != (3, 3).The SQL case runs with one process on purpose:
to_sqlwithnum_proc > 1and more than one batch fails on main withtable already existswhenever a later batch reaches the database before the first one, which is a separate defect I am reporting on its own.Fixes #8831