Repository navigation
[doc][data] Fix broken code examples in the Ray Data guides - #66636
Merged
Merged
Conversation
Fix examples that raise or don't match what they claim:
- loading-data: import gcsfs; pass the Azure filesystem by keyword
(read_parquet's filesystem is keyword-only); use the documented
dataset_id.table_id form for read_bigquery; drop the undefined
MongoDatasource() argument to write_mongo.
- saving-data: import gcsfs and adlfs.
- inspecting-data: make the print match the shown output.
- shuffling-data: key-based repartition example keyed on a column the
read_images dataset doesn't have; use ray.data.range and materialize.
- key-concepts: define the streaming example's placeholder UDFs and use
a 1K-row dataset to match its comment.
- scaling-collation-functions: pass the dataset the example reassigns.
- transforming-data: Polars example used an undefined name and two
nonexistent Polars APIs (from_pyarrow, summary); map_groups example
called group.drop("target"), which drops rows and raises KeyError.
Executed blocks now consume their datasets so the doctest runs the UDF.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Douglas Strodtman <[email protected]>
Contributor
There was a problem hiding this comment.
Code Review
This pull request updates several Ray Data documentation files to correct code snippets, add missing imports, fix Polars and Pandas API usages, and append .materialize() calls to ensure examples run properly. The feedback suggests renaming the row parameter to batch in the key concepts guide to accurately reflect that map_batches processes batches of data rather than individual rows.
matthewdeng
approved these changes
Oct 2, 2026
nadongjun
pushed a commit
to nadongjun/ray
that referenced
this pull request
Oct 6, 2026
…ect#66636) ## Why are these changes needed? A style pass over the Ray Data guides ([ray-project#66418](ray-project#66418), [ray-project#66419](ray-project#66419), [ray-project#66420](ray-project#66420), [ray-project#66422](ray-project#66422), [ray-project#66461](ray-project#66461)) turned up code examples that are broken as written. They raise if you copy them, or they don't match what the surrounding text says. This PR fixes only those examples. It doesn't convert untested blocks to tests. | Page | Problem | Fix | |---|---|---| | `loading-data.md` | GCS example uses `gcsfs` without importing it. | Import it. | | `loading-data.md` | Azure example passes `filesystem` positionally, but it's keyword-only in `read_parquet` (`TypeError`). | Pass it by keyword. | | `loading-data.md` | BigQuery `dataset="bigquery-public-data.ml_datasets.iris"`. `BigQueryDatasource` reads `dataset` as `dataset_id.table_id` under `project_id`, so this resolves to dataset `bigquery-public-data`, table `ml_datasets`. | Use a `my_dataset.my_table` placeholder, and say the format in the comment. | | `loading-data.md` | `write_mongo` example passes an undefined `MongoDatasource()`, and `write_mongo` has no such parameter. | Remove it. | | `saving-data.md` | GCS and Azure examples use `gcsfs` and `adlfs` without importing them. | Import them. | | `inspecting-data.md` | Output shows `Image shape:`, but the print has no colon. | Add the colon to the print. | | `shuffling-data.md` | Key-based repartition on `id`, but `ds` is a `read_images` dataset with only `image`. It passed only because nothing consumed the dataset. Consumed, it raises `KeyError: Field "id" does not exist`. | Use `ray.data.range(1000)` and materialize. | | `key-concepts.md` | Streaming example uses undefined UDF names and `read_parquet(...)`, under a "1K rows" comment. | Define placeholder UDFs and use `ray.data.range(1000)`. | | `scaling-collation-functions.md` | Example reassigns `dataset` but passes `train_dataset` to the trainer. | Use `train_dataset` throughout. | | `transforming-data.md` | Polars example: imports `pl` but calls `polars.` (`NameError`). `from_pyarrow` and `DataFrame.summary` don't exist in Polars. The block never consumed `ds`, so the UDF never ran. | `pl.from_arrow`, `describe()`, and materialize. | | `transforming-data.md` | pandas `map_groups` example calls `group.drop("target")`, which drops rows by label and raises `KeyError`. | `group.pop("target")`, which sets the column aside as the code intends. Materialize. | ## Duplicate check Searched open PRs for each affected API and page (`polars from_pyarrow`, `map_groups`, `gcsfs`, `read_bigquery`, `write_mongo`, `repartition`, collation). Nothing open addresses these examples. [ray-project#66614](ray-project#66614) and [ray-project#66615](ray-project#66615) edited some of the same pages, but not these examples. Both have merged, and this branch is rebased on them. ## Testing Ran against the Ray nightly wheel (`3.0.0.dev0`, `674900c91fa`) on macOS arm64, using the same pytest arguments and Data doctest plugin as the `doctest_each` targets in `doc/BUILD.bazel`, with `pytest==7.4.4` and Ray's `pytest-sphinx` fork: ```bash python -m pytest --doctest-modules --doctest-glob='*.md' --disable-warnings -c NO_PYTEST_CONFIG \ -p doctest_pytest_plugin doc/source/data/<page>.md ``` - `loading-data.md`, `saving-data.md`, `inspecting-data.md`, `shuffling-data.md`, and `key-concepts.md` pass, before and after the change. - `transforming-data.md` is in `doctest[data-gpu]` and stops at a CUDA block on a host without a GPU, before and after. So I ran its Iris setup block and the two changed blocks as a standalone doctest file, and it passes. Before the fix, those blocks raise `NameError` and `KeyError` once the dataset is consumed. - `key-concepts.md` streaming and `scaling-collation-functions.md` are untested `python` blocks. I ran the streaming block with `num_gpus=1` removed, and it runs. The collation change is a rename, checked by reading. - The `:skipif: True` GCS, Azure, BigQuery, and MongoDB blocks need credentials, so I checked them against the current signatures in `read_api.py` and `dataset.py` and `bigquery_datasource.py`. - `pre-commit run` passes on the changed files. This change touches only `.md` files, so premerge doesn't run the Data doc example tests. ## AI assistance Claude Code helped find the source for each fix, apply the edits, and run the tests. I reviewed every changed line. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Signed-off-by: Douglas Strodtman <[email protected]> Co-authored-by: Claude Opus 5.5 <[email protected]>
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.
Why are these changes needed?
A style pass over the Ray Data guides (#66418, #66419, #66420, #66422, #66461) turned up code examples that are broken as written. They raise if you copy them, or they don't match what the surrounding text says. This PR fixes only those examples. It doesn't convert untested blocks to tests.
loading-data.mdgcsfswithout importing it.loading-data.mdfilesystempositionally, but it's keyword-only inread_parquet(TypeError).loading-data.mddataset="bigquery-public-data.ml_datasets.iris".BigQueryDatasourcereadsdatasetasdataset_id.table_idunderproject_id, so this resolves to datasetbigquery-public-data, tableml_datasets.my_dataset.my_tableplaceholder, and say the format in the comment.loading-data.mdwrite_mongoexample passes an undefinedMongoDatasource(), andwrite_mongohas no such parameter.saving-data.mdgcsfsandadlfswithout importing them.inspecting-data.mdImage shape:, but the print has no colon.shuffling-data.mdid, butdsis aread_imagesdataset with onlyimage. It passed only because nothing consumed the dataset. Consumed, it raisesKeyError: Field "id" does not exist.ray.data.range(1000)and materialize.key-concepts.mdread_parquet(...), under a "1K rows" comment.ray.data.range(1000).scaling-collation-functions.mddatasetbut passestrain_datasetto the trainer.train_datasetthroughout.transforming-data.mdplbut callspolars.(NameError).from_pyarrowandDataFrame.summarydon't exist in Polars. The block never consumedds, so the UDF never ran.pl.from_arrow,describe(), and materialize.transforming-data.mdmap_groupsexample callsgroup.drop("target"), which drops rows by label and raisesKeyError.group.pop("target"), which sets the column aside as the code intends. Materialize.Duplicate check
Searched open PRs for each affected API and page (
polars from_pyarrow,map_groups,gcsfs,read_bigquery,write_mongo,repartition, collation). Nothing open addresses these examples. #66614 and #66615 edited some of the same pages, but not these examples. Both have merged, and this branch is rebased on them.Testing
Ran against the Ray nightly wheel (
3.0.0.dev0,674900c91fa) on macOS arm64, using the same pytest arguments and Data doctest plugin as thedoctest_eachtargets indoc/BUILD.bazel, withpytest==7.4.4and Ray'spytest-sphinxfork:loading-data.md,saving-data.md,inspecting-data.md,shuffling-data.md, andkey-concepts.mdpass, before and after the change.transforming-data.mdis indoctest[data-gpu]and stops at a CUDA block on a host without a GPU, before and after. So I ran its Iris setup block and the two changed blocks as a standalone doctest file, and it passes. Before the fix, those blocks raiseNameErrorandKeyErroronce the dataset is consumed.key-concepts.mdstreaming andscaling-collation-functions.mdare untestedpythonblocks. I ran the streaming block withnum_gpus=1removed, and it runs. The collation change is a rename, checked by reading.:skipif: TrueGCS, Azure, BigQuery, and MongoDB blocks need credentials, so I checked them against the current signatures inread_api.pyanddataset.pyandbigquery_datasource.py.pre-commit runpasses on the changed files.This change touches only
.mdfiles, so premerge doesn't run the Data doc example tests.AI assistance
Claude Code helped find the source for each fix, apply the edits, and run the tests. I reviewed every changed line.
🤖 Generated with Claude Code