Skip to content

BUG: fix broken error message in ujson.encode() (GH18878) #28666

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 6, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,7 @@ Other
- Using :meth:`DataFrame.replace` with overlapping keys in a nested dictionary will no longer raise, now matching the behavior of a flat dictionary (:issue:`27660`)
- :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`)
- :meth:`Series.append` will no longer raise a ``TypeError`` when passed a tuple of ``Series`` (:issue:`28410`)
- Fix corrupted error message when calling ``pandas.libs._json.encode()`` on a 0d array (:issue:`18878`)

.. _whatsnew_1000.contributors:

Expand Down
6 changes: 2 additions & 4 deletions pandas/_libs/src/ujson/python/objToJSON.c
Original file line number Diff line number Diff line change
Expand Up @@ -1986,11 +1986,9 @@ void Object_beginTypeContext(JSOBJ _obj, JSONTypeContext *tc) {
tc->type = JT_DOUBLE;
return;
} else if (PyArray_Check(obj) && PyArray_CheckScalar(obj)) {
tmpObj = PyObject_Repr(obj);
PyErr_Format(PyExc_TypeError,
"%s (0d array) is not JSON serializable at the moment",
PyBytes_AS_STRING(tmpObj));
Py_DECREF(tmpObj);
"%R (0d array) is not JSON serializable at the moment",
obj);
goto INVALID;
}

Expand Down
4 changes: 3 additions & 1 deletion pandas/tests/io/json/test_ujson.py
Original file line number Diff line number Diff line change
Expand Up @@ -780,8 +780,10 @@ def test_array_float(self):
tm.assert_almost_equal(arr, arr_out)

def test_0d_array(self):
with pytest.raises(TypeError):
msg = "array(1) (0d array) is not JSON serializable at the moment"
with pytest.raises(TypeError) as excinfo:
ujson.encode(np.array(1))
assert str(excinfo.value) == msg

@pytest.mark.parametrize(
"bad_input,exc_type,kwargs",
Expand Down