-
-
Notifications
You must be signed in to change notification settings - Fork 18.1k
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
Conversation
d0f2310
to
21f2689
Compare
21f2689
to
1c6781d
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for working on this
Thanks for taking your time to review this. I'm planing to open an issue discussing if this should be hidden behind a flag, such that the default behaviour remains unchanged. This draft is just a proof of concept. The decimal module has no C API, unfortunately. I found a recent issue in the cpython repo discussing an implementation but it hasn't happened yet. As far as I recall the maintainers of cpython thought this would bloat the C API without any real gain. I do agree on your comments and will update the patch. It should be noted that most of the implementation is copy/pasted from here. Maybe this has some bugs as-well? pandas/pandas/_libs/src/vendored/ujson/python/objToJSON.c Lines 352 to 374 in 7415aca
|
That's certainly possible. Ironically I am probably also the one responsible for that. Without looking at the blame...I'll just say the less I know, the better. Fixing that is out of scope for this PR, but I would prefer to not copy/paste those issues over. Thanks! |
@Tolker-KU generally this looks pretty good. Can you add a whatsnew note to our 3.0 release for this? |
…at() to suppress scientific notation
f3152b9
to
d60f71f
Compare
@@ -1467,8 +1488,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); |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This test should cover it:
GET_TC(tc)->newObj = str; | ||
|
||
Py_ssize_t s_len; | ||
char *outValue = (char *)PyUnicode_AsUTF8AndSize(str, &s_len); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would be ideal if our functions retuned const char*
so we didn't have to cast like this. A lot of this code is old and has changed hands many times, so not surprised there are some code smells...but if you are looking to continuing contributions on this, that cleanup would be great
decimal.Decimal
as full precision strings in .to_json(...)
decimal.Decimal
as full precision strings in .to_json(...)
@mroeschke mind taking a look? |
Thanks @Tolker-KU |
Encodes
decimal.Decimal
as full precision strings in.to_json(...)
. The old behaviour is to convertdecimal.Decimal
to floats potentially loosing precision.doc/source/whatsnew/vX.X.X.rst
file if fixing a bug or adding a new feature.