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

BUG: DataFrame.to_json OverflowError with np.long* dtypes #55495

Merged
merged 3 commits into from
Oct 16, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v2.2.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,7 @@ I/O
- Bug in :func:`read_csv` with ``engine="pyarrow"`` where ``usecols`` wasn't working with a csv with no headers (:issue:`54459`)
- Bug in :func:`read_excel`, with ``engine="xlrd"`` (``xls`` files) erroring when file contains NaNs/Infs (:issue:`54564`)
- Bug in :func:`to_excel`, with ``OdsWriter`` (``ods`` files) writing boolean/string value (:issue:`54994`)
- Bug in :meth:`DataFrame.to_json` OverflowError with np.long* dtypes (:issue:`55403`)
gupta-paras marked this conversation as resolved.
Show resolved Hide resolved
- Bug in :meth:`pandas.read_excel` with an ODS file without cached formatted cell for float values (:issue:`55219`)

Period
Expand Down
5 changes: 5 additions & 0 deletions pandas/_libs/src/vendored/ujson/python/objToJSON.c
Original file line number Diff line number Diff line change
Expand Up @@ -1610,6 +1610,11 @@ void Object_beginTypeContext(JSOBJ _obj, JSONTypeContext *tc) {
PyArray_DescrFromType(NPY_DOUBLE));
tc->type = JT_DOUBLE;
return;
} else if (PyArray_IsScalar(obj, LongDouble)) {
Copy link
Member

Choose a reason for hiding this comment

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

This works, but I think the lines directly following it are supposed to be a catchall for unsupported types. Do you know why that isn't being hit? I would rather we keep this generic instead of having to specify an error message for every type that we don't serialize

Copy link
Contributor Author

Choose a reason for hiding this comment

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

There is a code section at the end of this function:

    pc->iterBegin = Dir_iterBegin;
    pc->iterEnd = Dir_iterEnd;
    pc->iterNext = Dir_iterNext;
    pc->iterGetValue = Dir_iterGetValue;
    pc->iterGetName = Dir_iterGetName;
    return;

By default everything falls to this section and this causes infinite loop.

Copy link
Member

Choose a reason for hiding this comment

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

Right but I'm asking about the next branch after what you've added.

} else if (PyArray_Check(obj) && PyArray_CheckScalar(obj)) {

Do you know what hits that currently? From reading the function I think the intent of that was to generically catch the issue you've described, but its possible the invariant is incorrect. Would something pass both PyArray_Check and PyArray_CheckScalar? Maybe the PyArray_Check call is incorrect and removing that alone would fix your issue?

Copy link
Contributor Author

@gupta-paras gupta-paras Oct 13, 2023

Choose a reason for hiding this comment

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

Any value like: np.array(1) [0d array] evaluates true for both of them, so that check was intended for handling this case only. Also for any numpy scalar type, second will be true. So, simply removing pyArray_check handles both cases. Only thing we need to make sure, that all numpy scalar types are handled before this if block. I will add similar comment in code as well.
Thanks

PyErr_Format(PyExc_TypeError,
"%R (np.longdouble) is not JSON serializable at the moment",
obj);
goto INVALID;
} else if (PyArray_Check(obj) && PyArray_CheckScalar(obj)) {
PyErr_Format(PyExc_TypeError,
"%R (0d array) is not JSON serializable at the moment",
Expand Down
7 changes: 7 additions & 0 deletions pandas/tests/io/json/test_ujson.py
Original file line number Diff line number Diff line change
Expand Up @@ -818,6 +818,13 @@ def test_0d_array(self):
with pytest.raises(TypeError, match=msg):
ujson.ujson_dumps(np.array(1))

def test_array_long_double(self):
msg = re.compile(
"1234.5.*\\(np.longdouble\\) is not JSON serializable at the moment"
)
with pytest.raises(TypeError, match=msg):
ujson.ujson_dumps(np.longdouble(1234.5))


class TestPandasJSONTests:
def test_dataframe(self, orient):
Expand Down
Loading