Skip to content

Commit 32c9c8f

Browse files
authored
BUG: DataFrame.to_json OverflowError with np.long* dtypes (#55495)
* BUG: DataFrame.to_json OverflowError with np.long* dtypes * BUG: DataFrame.to_json OverflowError with np.long* dtypes #Comment-1 * BUG: DataFrame.to_json OverflowError with np.long* dtypes #Comment-2
1 parent ea14a46 commit 32c9c8f

File tree

3 files changed

+13
-3
lines changed

3 files changed

+13
-3
lines changed

doc/source/whatsnew/v2.2.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,7 @@ I/O
358358
- Bug in :func:`read_excel`, with ``engine="xlrd"`` (``xls`` files) erroring when file contains NaNs/Infs (:issue:`54564`)
359359
- Bug in :func:`to_excel`, with ``OdsWriter`` (``ods`` files) writing boolean/string value (:issue:`54994`)
360360
- Bug in :meth:`pandas.read_excel` with an ODS file without cached formatted cell for float values (:issue:`55219`)
361+
- Bug where :meth:`DataFrame.to_json` would raise an ``OverflowError`` instead of a ``TypeError`` with unsupported NumPy types (:issue:`55403`)
361362

362363
Period
363364
^^^^^^

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -1610,9 +1610,9 @@ void Object_beginTypeContext(JSOBJ _obj, JSONTypeContext *tc) {
16101610
PyArray_DescrFromType(NPY_DOUBLE));
16111611
tc->type = JT_DOUBLE;
16121612
return;
1613-
} else if (PyArray_Check(obj) && PyArray_CheckScalar(obj)) {
1613+
} else if (PyArray_CheckScalar(obj)) {
16141614
PyErr_Format(PyExc_TypeError,
1615-
"%R (0d array) is not JSON serializable at the moment",
1615+
"%R (numpy-scalar) is not JSON serializable at the moment",
16161616
obj);
16171617
goto INVALID;
16181618
} else if (object_is_na_type(obj)) {

pandas/tests/io/json/test_ujson.py

+10-1
Original file line numberDiff line numberDiff line change
@@ -814,10 +814,19 @@ 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

823+
def test_array_long_double(self):
824+
msg = re.compile(
825+
"1234.5.* \\(numpy-scalar\\) is not JSON serializable at the moment"
826+
)
827+
with pytest.raises(TypeError, match=msg):
828+
ujson.ujson_dumps(np.longdouble(1234.5))
829+
821830

822831
class TestPandasJSONTests:
823832
def test_dataframe(self, orient):

0 commit comments

Comments
 (0)