Pandas version checks
-
[x] I have checked that this issue has not already been reported.
-
[x] I have confirmed this bug exists on the latest version of pandas.
-
[x] 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
Comment From: Mudavath-Giri-Naik
@PentageerJoshuaMeetsma Hey can i work on this?
Comment From: Tuhin-SnapD
Can I implement a fix?
Comment From: santagatiivan
The float precision gets lost in DataFrame.to_json() because the default JSON encoder doesn’t keep enough decimal places, to fix it you can convert the floats to strings with high precision (for example, using format(value, '.17g')) before saving to JSON. This way, the values stay exact when you write and read the JSON back
Comment From: PentageerJoshuaMeetsma
The float precision gets lost in DataFrame.to_json() because the default JSON encoder doesn’t keep enough decimal places, to fix it you can convert the floats to strings with high precision (for example, using format(value, '.17g')) before saving to JSON. This way, the values stay exact when you write and read the JSON back
This is the solution I have been using, but it's definitely a bit of a hack. If I intend to save a floating point decimal using JSON, it seems to me that I should at very least be able to fully preserve that floating point decimal at full precision, and that should probably even be the default behaviour. What's the point of being able to serialize the data in a given format if the serialization does not actually preserve the data in that format with any integrity? Why not just have the .to_json() function just run the string conversion automatically, so that you can't save it as a decimal but can save it as a string, but are at least guaranteed to be able to retrieve your data with full integrity?
Comment From: PentageerJoshuaMeetsma
@PentageerJoshuaMeetsma Hey can i work on this?
Can I implement a fix?
I don't mind if anyone else wants to implement a fix, I was not planning on writing a fix myself.
I reported this issue because although the behaviour seems to be working as intended, I think the behaviour is still incorrect from what it should do, and by reporting this as a bug there can be a discussion about whether there should be a different behaviour or not. This is a bug report that is almost more about the philosophy of what JSON serialization is supposed to accomplish rather than a specific issue of implementation.