[Bug]: [Python] WriteToJson silently rounds floats to 10 decimal places
Maintainers usually reply within 1 day
Assessment
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Newbie friendliness
- 62/100
- Issue type
- Bug
- Clarity
- Clearly specified
- Activity status
- Active
- Tech stack
- json, pandas, python, yaml
- Domain
- data-engineering
Research direction
The rounding comes from the DataFrame path: WriteToJson in sdks/python/apache_beam/io/textio.py forwards kwargs to apache_beam.dataframe.io.to_json, which calls pandas to_json whose double_precision defaults to 10. Start by reproducing with the YAML pipeline in the issue, then check how textio.py builds the to_json call. Done means floats round-trip (a default double_precision or a json.dumps path for lines output) and a test in the textio or dataframe io tests covers 0.1234567890123.
Written by the indexing model from the issue text.
Description
What happened?
beam.io.WriteToJson (and Beam YAML WriteToJson) silently loses float precision: values are rounded to 10 decimal places.
| input | written |
|---|---|
0.1234567890123 |
0.123456789 |
3.3333333333333335 |
3.3333333333 |
12345678901.5 |
12345678901.5 |
There is no warning, so a JSON export doesn't round-trip and differs from the same data written with WriteToParquet, WriteToCsv or json.dumps.
Minimal reproducer (Beam YAML, Beam 2.75.0; WriteToJson on master @ 728ef1b4 has the same code path):
pipeline:
type: chain
transforms:
- type: Create
config:
elements:
- {id: 1, x: 0.1234567890123}
- {id: 2, x: 3.3333333333333335}
- type: WriteToJson
config:
path: /tmp/c5
python -m apache_beam.yaml.main --yaml_pipeline_file=c5.yaml writes:
{"id":1,"x":0.123456789}
{"id":2,"x":3.3333333333}
Cause
WriteToJson (sdks/python/apache_beam/io/textio.py) writes through the DataFrame API, apache_beam.dataframe.io.to_json -> pandas.DataFrame.to_json, and only forwards the caller's **kwargs. pandas' to_json defaults to double_precision=10, so every float is rounded to 10 decimal places.
Options:
- Default
double_precision=15(pandas' maximum) when the caller doesn't pass it. That fixes most cases but still isn't exact for 16-17 significant digits. - Write JSON lines with
json.dumpsfororient='records', lines=True(the YAML default), which round-trips floats exactly.
At minimum the docstring should mention the rounding and the double_precision kwarg. Happy to send a PR for whichever approach maintainers prefer.
Issue Priority
Priority: 2 (default / most bugs should be filed as P2)
Issue Components
- Component: Python SDK
- Component: Java SDK
- Component: Go SDK
- Component: Typescript SDK
- Component: IO connector
- Component: Beam YAML
- Component: Beam examples
- Component: Beam playground
- Component: Beam katas
- Component: Website
- Component: Infrastructure
- Component: Spark Runner
- Component: Flink Runner
- Component: Prism Runner
- Component: Twister2 Runner
- Component: Hazelcast Jet Runner
- Component: Google Cloud Dataflow Runner
- Dominant language
- Java
- Stars
- 8.7k
- Forks
- 4.8k
- Avg merge
- 2d 9h
- Merged PRs (30d)
- 368
Getting set up
- No Dockerfile or Docker Compose file
- Has a pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from apache/beam
-
awaiting triage bug P3
Difficulty 1/5 Under an hour Newbie friendliness 67/100
apache/beam#41197 · 2 comments ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
apache/beam#39624 · 2 reactions ·
Maintainers usually reply within 1 day
-
[Bug]: PubsubIO used in batch incorrect batch cutoff sizePossibly taken @1fanwang claimed this 78 days ago. Openbug io P3 pinned pubsub
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
apache/beam#28011 · 4 comments ·
Maintainers usually reply within 1 day
-
build P3 sub-task
Difficulty 1/5 Under an hour Newbie friendliness 68/100
Maintainers usually reply within 1 day
-
bug gcp io java P3
Difficulty 1/5 Under an hour Newbie friendliness 65/100
Maintainers usually reply within 1 day
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 66/100
SasanLabs/VulnerableApp#912 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
Maintainers usually reply within 1 day
-
desktop edt not responding
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
datafusion-contrib/StreamFusion#653 ·
Maintainers usually reply within 1 day
-
[Bug] Absolute row/column index 0 is ignored for images, hyperlinks and commentsPossibly taken @fanOfSunny claimed this today. Open
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
Maintainers usually reply within 1 day