Skip to content

Commit 2c5ed40

Browse files
rlamyproost
authored andcommitted
BUG: fix broken error message in ujson.encode() (GH18878) (pandas-dev#28666)
1 parent ff48f32 commit 2c5ed40

File tree

3 files changed

+6
-5
lines changed

3 files changed

+6
-5
lines changed

doc/source/whatsnew/v1.0.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,7 @@ Other
358358
- :meth:`DataFrame.to_csv` and :meth:`Series.to_csv` now support dicts as ``compression`` argument with key ``'method'`` being the compression method and others as additional compression options when the compression method is ``'zip'``. (:issue:`26023`)
359359
- Bug in :meth:`Series.diff` where a boolean series would incorrectly raise a ``TypeError`` (:issue:`17294`)
360360
- :meth:`Series.append` will no longer raise a ``TypeError`` when passed a tuple of ``Series`` (:issue:`28410`)
361+
- Fix corrupted error message when calling ``pandas.libs._json.encode()`` on a 0d array (:issue:`18878`)
361362

362363
.. _whatsnew_1000.contributors:
363364

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

+2-4
Original file line numberDiff line numberDiff line change
@@ -1986,11 +1986,9 @@ void Object_beginTypeContext(JSOBJ _obj, JSONTypeContext *tc) {
19861986
tc->type = JT_DOUBLE;
19871987
return;
19881988
} else if (PyArray_Check(obj) && PyArray_CheckScalar(obj)) {
1989-
tmpObj = PyObject_Repr(obj);
19901989
PyErr_Format(PyExc_TypeError,
1991-
"%s (0d array) is not JSON serializable at the moment",
1992-
PyBytes_AS_STRING(tmpObj));
1993-
Py_DECREF(tmpObj);
1990+
"%R (0d array) is not JSON serializable at the moment",
1991+
obj);
19941992
goto INVALID;
19951993
}
19961994

pandas/tests/io/json/test_ujson.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -780,7 +780,9 @@ def test_array_float(self):
780780
tm.assert_almost_equal(arr, arr_out)
781781

782782
def test_0d_array(self):
783-
with pytest.raises(TypeError):
783+
# gh-18878
784+
msg = re.escape("array(1) (0d array) is not JSON serializable at the moment")
785+
with pytest.raises(TypeError, match=msg):
784786
ujson.encode(np.array(1))
785787

786788
@pytest.mark.parametrize(

0 commit comments

Comments
 (0)