Skip to content

Commit b972f99

Browse files
committed
BUG: DataFrame.to_json OverflowError with np.long* dtypes
1 parent b284101 commit b972f99

File tree

3 files changed

+11
-0
lines changed

3 files changed

+11
-0
lines changed

doc/source/whatsnew/v2.2.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,7 @@ I/O
348348
- Bug in :func:`read_csv` with ``engine="pyarrow"`` where ``usecols`` wasn't working with a csv with no headers (:issue:`54459`)
349349
- Bug in :func:`read_excel`, with ``engine="xlrd"`` (``xls`` files) erroring when file contains NaNs/Infs (:issue:`54564`)
350350
- Bug in :func:`to_excel`, with ``OdsWriter`` (``ods`` files) writing boolean/string value (:issue:`54994`)
351+
- Bug in :meth:`DataFrame.to_json` OverflowError with np.long* dtypes (:issue:`55403`)
351352
- Bug in :meth:`pandas.read_excel` with an ODS file without cached formatted cell for float values (:issue:`55219`)
352353

353354
Period

pandas/_libs/src/vendored/ujson/python/objToJSON.c

+5
Original file line numberDiff line numberDiff line change
@@ -1610,6 +1610,11 @@ void Object_beginTypeContext(JSOBJ _obj, JSONTypeContext *tc) {
16101610
PyArray_DescrFromType(NPY_DOUBLE));
16111611
tc->type = JT_DOUBLE;
16121612
return;
1613+
} else if (PyArray_IsScalar(obj, LongDouble)) {
1614+
PyErr_Format(PyExc_TypeError,
1615+
"%R (np.longdouble) is not JSON serializable at the moment",
1616+
obj);
1617+
goto INVALID;
16131618
} else if (PyArray_Check(obj) && PyArray_CheckScalar(obj)) {
16141619
PyErr_Format(PyExc_TypeError,
16151620
"%R (0d array) is not JSON serializable at the moment",

pandas/tests/io/json/test_ujson.py

+5
Original file line numberDiff line numberDiff line change
@@ -818,6 +818,11 @@ def test_0d_array(self):
818818
with pytest.raises(TypeError, match=msg):
819819
ujson.ujson_dumps(np.array(1))
820820

821+
def test_array_long_double(self):
822+
msg = re.escape("1.0 (np.longdouble) is not JSON serializable at the moment")
823+
with pytest.raises(TypeError, match=msg):
824+
ujson.ujson_dumps(np.longdouble(1))
825+
821826

822827
class TestPandasJSONTests:
823828
def test_dataframe(self, orient):

0 commit comments

Comments
 (0)