[Bug]: [Python] WriteToJson silently rounds floats to 10 decimal places

Aperta Adatta ai principianti
#41,102 4 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

@Abhi2939 ci sta già lavorando.

Dal 10/11/2026.

  • #41154 di @Abhi2939 — aperta

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
62/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
json, pandas, python, yaml

Direzione di ricerca

L'arrotondamento deriva dal percorso DataFrame: WriteToJson in sdks/python/apache_beam/io/textio.py inoltra i kwargs ad apache_beam.dataframe.io.to_json, che chiama to_json di pandas, il cui double_precision ha come valore predefinito 10. Inizia riproducendo il problema con la pipeline YAML della issue, poi controlla come textio.py costruisce la chiamata a to_json. È fatto quando i float fanno un round-trip senza perdita (un valore predefinito adeguato per double_precision o un percorso json.dumps per l'output a righe) e un test nei test di textio o di dataframe io copre 0.1234567890123.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

io P2 python yaml
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:

  1. 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.
  2. Write JSON lines with json.dumps for orient='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
Lingua principale
Java
Stelle
8.7k
Fork
4.8k
Merge medio
2g 9h
PR unite (30g)
368

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di apache/beam

Tutte le issue di apache/beam

Issue simili

Altre issue su Java

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.