From b972f99f7f763213a7606acbe1b880428e455cb6 Mon Sep 17 00:00:00 2001 From: gupta-paras Date: Thu, 12 Oct 2023 17:53:36 +0530 Subject: [PATCH] BUG: DataFrame.to_json OverflowError with np.long* dtypes --- doc/source/whatsnew/v2.2.0.rst | 1 + pandas/_libs/src/vendored/ujson/python/objToJSON.c | 5 +++++ pandas/tests/io/json/test_ujson.py | 5 +++++ 3 files changed, 11 insertions(+) diff --git a/doc/source/whatsnew/v2.2.0.rst b/doc/source/whatsnew/v2.2.0.rst index 29a2d5c0b5877d..a5e96a512b2df2 100644 --- a/doc/source/whatsnew/v2.2.0.rst +++ b/doc/source/whatsnew/v2.2.0.rst @@ -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`) - Bug in :meth:`pandas.read_excel` with an ODS file without cached formatted cell for float values (:issue:`55219`) Period diff --git a/pandas/_libs/src/vendored/ujson/python/objToJSON.c b/pandas/_libs/src/vendored/ujson/python/objToJSON.c index 8c55505f61b514..71a7cfc792b83c 100644 --- a/pandas/_libs/src/vendored/ujson/python/objToJSON.c +++ b/pandas/_libs/src/vendored/ujson/python/objToJSON.c @@ -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)) { + 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", diff --git a/pandas/tests/io/json/test_ujson.py b/pandas/tests/io/json/test_ujson.py index d5f8c5200c4a37..2b6e2722049547 100644 --- a/pandas/tests/io/json/test_ujson.py +++ b/pandas/tests/io/json/test_ujson.py @@ -818,6 +818,11 @@ 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.escape("1.0 (np.longdouble) is not JSON serializable at the moment") + with pytest.raises(TypeError, match=msg): + ujson.ujson_dumps(np.longdouble(1)) + class TestPandasJSONTests: def test_dataframe(self, orient):