Skip to content

BUG: DataFrame.to_json() does not accurately serialize floats such that they can be deserialized without lost information #62072

@PentageerJoshuaMeetsma

Description

@PentageerJoshuaMeetsma

Pandas version checks

  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of pandas.

  • I have confirmed this bug exists on the main branch of pandas.

Reproducible Example

import pandas as pd
df = pd.DataFrame({"x":[0.2617993877991494,0.111111111111111112]}) # two such examples that result in incorrect truncation
df.to_json("out.json",double_precision=15)
df2 = pd.read_json("out.json")

Issue Description

Using the DataFrame.to_json() method to serialize a DataFrame can result in lost data when serializing floats, such that the data cannot be recovered when reloading the json file, even when using the maximum allowable double_precision parameter, at 15.

This is the result of the to_json() method incorrectly truncating floats when they should instead be reproduced in full. This seems to perhaps even be an intended behaviour, as the default value of the double_precision parameter is not even 15, but 10, resulting in even further truncation and lost data. This should not be the case, as the output of the json format stores all numbers as text strings, so there is not an inherent loss in data from the format, and a user should reasonably be able to fully retrieve an exact copy of the data they have saved in the json format at a later time.

Expected Behavior

df = pd.DataFrame({"x":[0.2617993877991494,0.111111111111111112]})
df.to_json("out.json",double_precision=15)
df2 = pd.read_json("out.json")
print(df["x"][0],df["x"][1])
# output:          0.2617993877991494 0.11111111111111112
print(df2["x"][0],df2["x"][1])
# expected output: 0.2617993877991494 0.11111111111111112
# actual output:   0.261799387799149 0.11111111111111101
print(df["x"][0]==df2["x"][0])
# expected output: True (as all we have done is saved the data to the json format and reloaded it)
# actual output :  False
print(df["x"][1]==df2["x"][1])
# expected output: True (as all we have done is saved the data to the json format and reloaded it)
# actual output :  False

Installed Versions

INSTALLED VERSIONS

commit : 0691c5c
python : 3.12.2
python-bits : 64
OS : Windows
OS-release : 11
Version : 10.0.26100
machine : AMD64
processor : Intel64 Family 6 Model 186 Stepping 2, GenuineIntel
byteorder : little
LC_ALL : None
LANG : None
LOCALE : English_Canada.1252

pandas : 2.2.3
numpy : 2.2.6
pytz : 2025.2
dateutil : 2.9.0.post0
pip : 25.2
Cython : None
sphinx : 8.2.3
IPython : 9.3.0
adbc-driver-postgresql: None
adbc-driver-sqlite : None
bs4 : 4.13.4
blosc : None
bottleneck : None
dataframe-api-compat : None
fastparquet : None
fsspec : None
html5lib : None
hypothesis : None
gcsfs : None
jinja2 : 3.1.6
lxml.etree : None
matplotlib : 3.10.3
numba : None
numexpr : None
odfpy : None
openpyxl : 3.1.5
pandas_gbq : None
psycopg2 : None
pymysql : None
pyarrow : None
pyreadstat : None
pytest : None
python-calamine : None
pyxlsb : None
s3fs : None
scipy : 1.15.3
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlsxwriter : None
zstandard : None
tzdata : 2025.2
qtpy : None
pyqt5 : None

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugNeeds TriageIssue that has not been reviewed by a pandas team member

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions