Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ENH: Format decimal.Decimal as full precision strings in .to_json(...) #60698

Merged
merged 4 commits into from
Jan 14, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 37 additions & 2 deletions pandas/_libs/src/vendored/ujson/python/objToJSON.c
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,31 @@ static char *PyTimeToJSON(JSOBJ _obj, JSONTypeContext *tc, size_t *outLen) {
return outValue;
}

static char *PyDecimalToUTF8Callback(JSOBJ _obj, JSONTypeContext *tc,
size_t *len) {
PyObject *obj = (PyObject *)_obj;
PyObject *str = PyObject_Str(obj);
Tolker-KU marked this conversation as resolved.
Show resolved Hide resolved
if (str == NULL) {
*len = 0;
Tolker-KU marked this conversation as resolved.
Show resolved Hide resolved
if (!PyErr_Occurred()) {
Tolker-KU marked this conversation as resolved.
Show resolved Hide resolved
PyErr_SetString(PyExc_ValueError, "Failed to convert decimal");
}
((JSONObjectEncoder *)tc->encoder)->errorMsg = "";
Tolker-KU marked this conversation as resolved.
Show resolved Hide resolved
return NULL;
}
if (PyUnicode_Check(str)) {
Tolker-KU marked this conversation as resolved.
Show resolved Hide resolved
PyObject *tmp = str;
str = PyUnicode_AsUTF8String(str);
Tolker-KU marked this conversation as resolved.
Show resolved Hide resolved
Py_DECREF(tmp);
}

GET_TC(tc)->newObj = str;

*len = PyBytes_GET_SIZE(str);
char *outValue = PyBytes_AS_STRING(str);
return outValue;
}

//=============================================================================
// Numpy array iteration functions
//=============================================================================
Expand Down Expand Up @@ -1467,8 +1492,18 @@ static void Object_beginTypeContext(JSOBJ _obj, JSONTypeContext *tc) {
tc->type = JT_UTF8;
return;
} else if (object_is_decimal_type(obj)) {
pc->doubleValue = PyFloat_AsDouble(obj);
tc->type = JT_DOUBLE;
PyObject *is_nan_py = PyObject_RichCompare(obj, obj, Py_NE);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do the test cases cover this code? If not, can you add a NaN case?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if (is_nan_py == NULL) {
goto INVALID;
}
int is_nan = (is_nan_py == Py_True);
Py_DECREF(is_nan_py);
if (is_nan) {
tc->type = JT_NULL;
return;
}
pc->PyTypeToUTF8 = PyDecimalToUTF8Callback;
tc->type = JT_UTF8;
return;
} else if (PyDateTime_Check(obj) || PyDate_Check(obj)) {
if (object_is_nat_type(obj)) {
Expand Down
4 changes: 2 additions & 2 deletions pandas/tests/io/json/test_json_table_schema_ext_dtype.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ def test_build_decimal_series(self, dc):
expected = OrderedDict(
[
("schema", schema),
("data", [OrderedDict([("id", 0), ("a", 10.0)])]),
("data", [OrderedDict([("id", 0), ("a", "10")])]),
]
)

Expand Down Expand Up @@ -245,7 +245,7 @@ def test_to_json(self, da, dc, sa, ia):
[
("idx", 0),
("A", "2021-10-10T00:00:00.000"),
("B", 10.0),
("B", "10"),
("C", "pandas"),
("D", 10),
]
Expand Down
7 changes: 1 addition & 6 deletions pandas/tests/io/json/test_pandas.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import datetime
from datetime import timedelta
from decimal import Decimal
from io import StringIO
import json
import os
Expand Down Expand Up @@ -2025,12 +2024,8 @@ def test_to_s3(self, s3_public_bucket, s3so):
timeout -= 0.1
assert timeout > 0, "Timed out waiting for file to appear on moto"

def test_json_pandas_nulls(self, nulls_fixture, request):
def test_json_pandas_nulls(self, nulls_fixture):
# GH 31615
if isinstance(nulls_fixture, Decimal):
mark = pytest.mark.xfail(reason="not implemented")
request.applymarker(mark)

expected_warning = None
msg = (
"The default 'epoch' date format is deprecated and will be removed "
Expand Down
30 changes: 15 additions & 15 deletions pandas/tests/io/json/test_ujson.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,56 +57,56 @@ def test_encode_decimal(self):
sut = decimal.Decimal("1337.1337")
encoded = ujson.ujson_dumps(sut, double_precision=15)
decoded = ujson.ujson_loads(encoded)
assert decoded == 1337.1337
assert decoded == "1337.1337"

sut = decimal.Decimal("0.95")
encoded = ujson.ujson_dumps(sut, double_precision=1)
assert encoded == "1.0"
assert encoded == '"0.95"'

decoded = ujson.ujson_loads(encoded)
assert decoded == 1.0
assert decoded == "0.95"

sut = decimal.Decimal("0.94")
encoded = ujson.ujson_dumps(sut, double_precision=1)
assert encoded == "0.9"
assert encoded == '"0.94"'

decoded = ujson.ujson_loads(encoded)
assert decoded == 0.9
assert decoded == "0.94"

sut = decimal.Decimal("1.95")
encoded = ujson.ujson_dumps(sut, double_precision=1)
assert encoded == "2.0"
assert encoded == '"1.95"'

decoded = ujson.ujson_loads(encoded)
assert decoded == 2.0
assert decoded == "1.95"

sut = decimal.Decimal("-1.95")
encoded = ujson.ujson_dumps(sut, double_precision=1)
assert encoded == "-2.0"
assert encoded == '"-1.95"'

decoded = ujson.ujson_loads(encoded)
assert decoded == -2.0
assert decoded == "-1.95"

sut = decimal.Decimal("0.995")
encoded = ujson.ujson_dumps(sut, double_precision=2)
assert encoded == "1.0"
assert encoded == '"0.995"'

decoded = ujson.ujson_loads(encoded)
assert decoded == 1.0
assert decoded == "0.995"

sut = decimal.Decimal("0.9995")
encoded = ujson.ujson_dumps(sut, double_precision=3)
assert encoded == "1.0"
assert encoded == '"0.9995"'

decoded = ujson.ujson_loads(encoded)
assert decoded == 1.0
assert decoded == "0.9995"

sut = decimal.Decimal("0.99999999999999944")
encoded = ujson.ujson_dumps(sut, double_precision=15)
assert encoded == "1.0"
assert encoded == '"0.99999999999999944"'

decoded = ujson.ujson_loads(encoded)
assert decoded == 1.0
assert decoded == "0.99999999999999944"

@pytest.mark.parametrize("ensure_ascii", [True, False])
def test_encode_string_conversion(self, ensure_ascii):
Expand Down