Skip to content

Commit 7a29022

Browse files
committed
BUG: DataFrame.to_json OverflowError with np.long* dtypes #Comment-1
1 parent 3e3c713 commit 7a29022

File tree

2 files changed

+10
-9
lines changed

2 files changed

+10
-9
lines changed

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

+6-7
Original file line numberDiff line numberDiff line change
@@ -1610,14 +1610,13 @@ 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)) {
1613+
} else if (PyArray_CheckScalar(obj)) {
1614+
/* This handles all cases of array of zero dimension (numpy.array(1)) OR
1615+
unimplemented serializable for numpy scalar type like: numpy.longdouble.
1616+
If we plan to handle any other numpy type for serializations,
1617+
we need to keep it above this block. */
16141618
PyErr_Format(PyExc_TypeError,
1615-
"%R (np.longdouble) is not JSON serializable at the moment",
1616-
obj);
1617-
goto INVALID;
1618-
} else if (PyArray_Check(obj) && PyArray_CheckScalar(obj)) {
1619-
PyErr_Format(PyExc_TypeError,
1620-
"%R (0d array) is not JSON serializable at the moment",
1619+
"%R (numpy-scalar) is not JSON serializable at the moment",
16211620
obj);
16221621
goto INVALID;
16231622
} else if (object_is_na_type(obj)) {

pandas/tests/io/json/test_ujson.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -814,13 +814,15 @@ def test_array_float(self):
814814

815815
def test_0d_array(self):
816816
# gh-18878
817-
msg = re.escape("array(1) (0d array) is not JSON serializable at the moment")
817+
msg = re.escape(
818+
"array(1) (numpy-scalar) is not JSON serializable at the moment"
819+
)
818820
with pytest.raises(TypeError, match=msg):
819821
ujson.ujson_dumps(np.array(1))
820822

821823
def test_array_long_double(self):
822824
msg = re.compile(
823-
"1234.5.*\\(np.longdouble\\) is not JSON serializable at the moment"
825+
"1234.5.* \\(numpy-scalar\\) is not JSON serializable at the moment"
824826
)
825827
with pytest.raises(TypeError, match=msg):
826828
ujson.ujson_dumps(np.longdouble(1234.5))

0 commit comments

Comments
 (0)