Skip to content

Commit

Permalink
BUG: DataFrame.to_json OverflowError with np.long* dtypes (pandas-dev…
Browse files Browse the repository at this point in the history
…#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
  • Loading branch information
gupta-paras authored Oct 16, 2023
1 parent ea14a46 commit 32c9c8f
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 3 deletions.
1 change: 1 addition & 0 deletions doc/source/whatsnew/v2.2.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,7 @@ I/O
- Bug in :func:`read_excel`, with ``engine="xlrd"`` (``xls`` files) erroring when file contains NaNs/Infs (:issue:`54564`)
- Bug in :func:`to_excel`, with ``OdsWriter`` (``ods`` files) writing boolean/string value (:issue:`54994`)
- Bug in :meth:`pandas.read_excel` with an ODS file without cached formatted cell for float values (:issue:`55219`)
- Bug where :meth:`DataFrame.to_json` would raise an ``OverflowError`` instead of a ``TypeError`` with unsupported NumPy types (:issue:`55403`)

Period
^^^^^^
Expand Down
4 changes: 2 additions & 2 deletions pandas/_libs/src/vendored/ujson/python/objToJSON.c
Original file line number Diff line number Diff line change
Expand Up @@ -1610,9 +1610,9 @@ void Object_beginTypeContext(JSOBJ _obj, JSONTypeContext *tc) {
PyArray_DescrFromType(NPY_DOUBLE));
tc->type = JT_DOUBLE;
return;
} else if (PyArray_Check(obj) && PyArray_CheckScalar(obj)) {
} else if (PyArray_CheckScalar(obj)) {
PyErr_Format(PyExc_TypeError,
"%R (0d array) is not JSON serializable at the moment",
"%R (numpy-scalar) is not JSON serializable at the moment",
obj);
goto INVALID;
} else if (object_is_na_type(obj)) {
Expand Down
11 changes: 10 additions & 1 deletion pandas/tests/io/json/test_ujson.py
Original file line number Diff line number Diff line change
Expand Up @@ -814,10 +814,19 @@ def test_array_float(self):

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

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


class TestPandasJSONTests:
def test_dataframe(self, orient):
Expand Down

0 comments on commit 32c9c8f

Please sign in to comment.