Support batched=True in Dataset.to_dict - #8333
Merged
Merged
Conversation
Dataset.to_dict(batched=True, batch_size=...) ignored both arguments and always returned the full dict, contradicting its signature, docstring, and Union[dict, Iterator[dict]] return type. Iterating the promised generator therefore walked the dict keys and raised. The sibling to_pandas/to_polars already implement batching; mirror that here, using a small local closure so the per-batch JSON-field decoding is not duplicated across both branches.
|
The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update. |
Member
|
can you run |
Contributor
Author
|
Done, ran |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
What does this do?
Dataset.to_dict(batch_size=..., batched=True)silently ignores both arguments and always returns the full dict, even though its signature, docstring ("Set toTrueto return a generator that yields the dataset as batches"), and-> Union[dict, Iterator[dict]]return type all promise batching. The sibling methodsto_pandasandto_polarsimplement batching;to_dictwas missing the branch.Because it returns a plain
dictinstead of the promised generator, iterating the result as documented walks the dict keys and raises:Fix
Mirror the
to_pandas/to_polarsstructure: return the full dict whenbatched=False, otherwise return a generator that yieldsbatch_size-row slices (defaultingbatch_sizetoconfig.DEFAULT_MAX_BATCH_SIZE). A small local closure keeps the per-batch JSON-field decoding from being duplicated across the two branches.After the fix:
Backward compatible:
batcheddefaults toFalse, so every existing.to_dict()call still returns a plain dict (verified byte-identical). Index mapping (select(...)) andJson()-feature decoding both work in batched and non-batched modes.Test
Added a "Batched" block to
tests/test_arrow_dataset.py::test_to_dict, mirroring the existingtest_to_pandas/test_to_polarscoverage: it asserts each yielded batch is adictwith the right columns and no more thanbatch_sizerows. It fails before this change (the returned dict is iterated as keys) and passes after.