Skip to content

BUG: Fixed encoding of pd.NA with to_json #31748

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
Show file tree
Hide file tree
Changes from all 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
5 changes: 3 additions & 2 deletions doc/source/whatsnew/v1.0.2.rst
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ Fixed regressions
Bug fixes
~~~~~~~~~

-
-
**I/O**
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @ArtificialQualia

This should be

I/O
^^^

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@MarcoGorelli I've updated that to the correct format.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The original was actually fine, to be consistent with the v1.0.1 file

(sorry for the conflicting comments)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My mistake, sorry, I'd taken that from v1.1.0 where we go back to

I/O
^^^

, will be more careful in the future

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@MarcoGorelli no problem! It's also inconsistent between the different files .. (for the bug fix releases, the list of bugs is much smaller, so less worth it to make subsections. At least, that is what we just did for the v1.0.1 file)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reverted to original format


- Using ``pd.NA`` with :meth:`DataFrame.to_json` now correctly outputs a null value instead of an empty object (:issue:`31615`)

.. ---------------------------------------------------------------------------

Expand Down
12 changes: 12 additions & 0 deletions pandas/_libs/src/ujson/python/objToJSON.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ static PyTypeObject *cls_dataframe;
static PyTypeObject *cls_series;
static PyTypeObject *cls_index;
static PyTypeObject *cls_nat;
static PyTypeObject *cls_na;
PyObject *cls_timedelta;

npy_int64 get_nat(void) { return NPY_MIN_INT64; }
Expand Down Expand Up @@ -149,6 +150,7 @@ int PdBlock_iterNext(JSOBJ, JSONTypeContext *);
void *initObjToJSON(void) {
PyObject *mod_pandas;
PyObject *mod_nattype;
PyObject *mod_natype;
PyObject *mod_decimal = PyImport_ImportModule("decimal");
type_decimal =
(PyTypeObject *)PyObject_GetAttrString(mod_decimal, "Decimal");
Expand All @@ -174,6 +176,12 @@ void *initObjToJSON(void) {
Py_DECREF(mod_nattype);
}

mod_natype = PyImport_ImportModule("pandas._libs.missing");
if (mod_natype) {
cls_na = (PyTypeObject *)PyObject_GetAttrString(mod_natype, "NAType");
Py_DECREF(mod_natype);
}

/* Initialise numpy API */
import_array();
// GH 31463
Expand Down Expand Up @@ -1789,6 +1797,10 @@ void Object_beginTypeContext(JSOBJ _obj, JSONTypeContext *tc) {
"%R (0d array) is not JSON serializable at the moment",
obj);
goto INVALID;
} else if (PyObject_TypeCheck(obj, cls_na)) {
PRINTMARK();
tc->type = JT_NULL;
return;
}

ISITERABLE:
Expand Down
10 changes: 10 additions & 0 deletions pandas/tests/io/json/test_pandas.py
Original file line number Diff line number Diff line change
Expand Up @@ -1671,3 +1671,13 @@ def test_to_s3(self, s3_resource):
assert target_file in (
obj.key for obj in s3_resource.Bucket("pandas-test").objects.all()
)

def test_json_pandas_na(self):
# GH 31615
result = pd.DataFrame([[pd.NA]]).to_json()
assert result == '{"0":{"0":null}}'

def test_json_pandas_nulls(self, nulls_fixture):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@simonjayhawkins do you know of an easy way to add pd.NA to nulls_fixture inline during injection? Or would adding another fixture on top be the only way?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since we already have two nulls_fixtures (nulls_fixture and unique_nulls_fixture), I think adding another could be the simplest way for now.

I think, in general, simpler to skip than to add a extra parameter (although I've not kept up with the latest pytest releases).

There is not a huge number of tests using the nulls_fixture and maybe tests that do should also test for pandas.NA, either now of in the future. So an alternative could be to add pd.NA to the existing nulls fixture and either skip of xfail.

# GH 31615
result = pd.DataFrame([[nulls_fixture]]).to_json()
assert result == '{"0":{"0":null}}'