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

Offen Anfängerfreundlich
#41,102 4 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Maintainer antworten meist innerhalb von 1 Tag

@Abhi2939 arbeitet bereits daran.

Seit 10.11.2026.

  • #41154 von @Abhi2939 — offen

Bewertung

Schwierigkeit
2/5
Geschätzter Aufwand
1-3 Stunden
Anfängerfreundlichkeit
62/100
Issue-Typ
Bug
Klarheit
Klar beschrieben
Aktivitätsstatus
Aktiv
Tech-Stack
json, pandas, python, yaml

Rechercherichtung

Das Runden stammt aus dem DataFrame-Pfad: WriteToJson in sdks/python/apache_beam/io/textio.py leitet kwargs an apache_beam.dataframe.io.to_json weiter, das pandas to_json aufruft, dessen double_precision standardmäßig 10 ist. Beginne damit, mit der YAML-Pipeline aus dem Issue zu reproduzieren, und prüfe dann, wie textio.py den to_json-Aufruf zusammenbaut. Fertig ist, wenn Floats unverändert ein Round-Trip durchlaufen (ein angepasster Standardwert für double_precision oder ein json.dumps-Pfad für Zeilenausgabe) und ein Test in den textio- oder dataframe-io-Tests den Wert 0.1234567890123 abdeckt.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Beschreibung

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
Vorherrschende Sprache
Java
Sterne
8.7k
Forks
4.8k
Ø Merge
2 T. 12 Std.
Gemergte PRs (30 T.)
371

Entwicklungsumgebung

Erste Schritte

  1. Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
  3. Forken Sie das Repository und arbeiten Sie in einem Branch.
  4. Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.

Mehr aus apache/beam

Alle Issues in apache/beam

Ähnliche Issues

Weitere Issues zu Java

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.