From 04425d4db44fec076237565d8e1ec63d94291274 Mon Sep 17 00:00:00 2001 From: Robert de Vries Date: Mon, 20 Jun 2022 17:11:05 +0200 Subject: [PATCH 1/6] Fix segmentation fault when JSON serializing a PeriodIndex Fixes #46683 --- pandas/_libs/src/ujson/python/objToJSON.c | 3 ++- pandas/tests/io/json/test_ujson.py | 7 +++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/pandas/_libs/src/ujson/python/objToJSON.c b/pandas/_libs/src/ujson/python/objToJSON.c index 73d2a1f786f8b..a2b04152f08e2 100644 --- a/pandas/_libs/src/ujson/python/objToJSON.c +++ b/pandas/_libs/src/ujson/python/objToJSON.c @@ -238,8 +238,9 @@ static PyObject *get_values(PyObject *obj) { PyErr_Clear(); } else if (PyObject_HasAttrString(values, "__array__")) { // We may have gotten a Categorical or Sparse array so call np.array + PyObject *array_values = PyObject_CallMethod(values, "__array__", NULL); Py_DECREF(values); - values = PyObject_CallMethod(values, "__array__", NULL); + values = array_values; } else if (!PyArray_CheckExact(values)) { // Didn't get a numpy array, so keep trying Py_DECREF(values); diff --git a/pandas/tests/io/json/test_ujson.py b/pandas/tests/io/json/test_ujson.py index e82a888f47388..ae13d8d5fb180 100644 --- a/pandas/tests/io/json/test_ujson.py +++ b/pandas/tests/io/json/test_ujson.py @@ -23,6 +23,7 @@ DatetimeIndex, Index, NaT, + PeriodIndex, Series, Timedelta, Timestamp, @@ -1240,3 +1241,9 @@ def test_encode_timedelta_iso(self, td): expected = f'"{td.isoformat()}"' assert result == expected + + def test_encode_periodindex(self): + # GH 46683 + p = PeriodIndex(["2022-04-06", "2022-04-07"], freq="D") + df = DataFrame(index=p) + assert df.to_json() == "{}" From bc6d32ab89d79f0afda909fe01bb7e4c9e9307bc Mon Sep 17 00:00:00 2001 From: Robert de Vries Date: Mon, 20 Jun 2022 17:44:06 +0200 Subject: [PATCH 2/6] Fix cpplint issues --- pandas/_libs/src/ujson/python/objToJSON.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pandas/_libs/src/ujson/python/objToJSON.c b/pandas/_libs/src/ujson/python/objToJSON.c index a2b04152f08e2..260f1ffb6165f 100644 --- a/pandas/_libs/src/ujson/python/objToJSON.c +++ b/pandas/_libs/src/ujson/python/objToJSON.c @@ -238,7 +238,8 @@ static PyObject *get_values(PyObject *obj) { PyErr_Clear(); } else if (PyObject_HasAttrString(values, "__array__")) { // We may have gotten a Categorical or Sparse array so call np.array - PyObject *array_values = PyObject_CallMethod(values, "__array__", NULL); + PyObject *array_values = PyObject_CallMethod(values, "__array__", + NULL); Py_DECREF(values); values = array_values; } else if (!PyArray_CheckExact(values)) { From 60a0910b271bb13218fcaedac8203e4243a9b57f Mon Sep 17 00:00:00 2001 From: Robert de Vries Date: Mon, 20 Jun 2022 17:50:20 +0200 Subject: [PATCH 3/6] Add whatsnew entry --- doc/source/whatsnew/v1.4.3.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/doc/source/whatsnew/v1.4.3.rst b/doc/source/whatsnew/v1.4.3.rst index a4d81533df23d..501700714bd32 100644 --- a/doc/source/whatsnew/v1.4.3.rst +++ b/doc/source/whatsnew/v1.4.3.rst @@ -30,6 +30,7 @@ Fixed regressions - Fixed regression in :func:`assert_index_equal` when ``check_order=False`` and :class:`Index` has extension or object dtype (:issue:`47207`) - Fixed regression in :func:`read_excel` returning ints as floats on certain input sheets (:issue:`46988`) - Fixed regression in :meth:`DataFrame.shift` when ``axis`` is ``columns`` and ``fill_value`` is absent, ``freq`` is ignored (:issue:`47039`) +- Fixed regression in :meth:`DataFrame.to_json` when ``index`` is of the type ``PeriodIndex`` causing a segmentation violation (:issue:`46683`) .. --------------------------------------------------------------------------- From 02f6e815150d88ee752139433a29f3b16ed4f44b Mon Sep 17 00:00:00 2001 From: Robert de Vries Date: Mon, 20 Jun 2022 21:56:34 +0200 Subject: [PATCH 4/6] Address review comment to annotate PeriodIndex as a class --- doc/source/whatsnew/v1.4.3.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v1.4.3.rst b/doc/source/whatsnew/v1.4.3.rst index 501700714bd32..58a6121a370b3 100644 --- a/doc/source/whatsnew/v1.4.3.rst +++ b/doc/source/whatsnew/v1.4.3.rst @@ -30,7 +30,7 @@ Fixed regressions - Fixed regression in :func:`assert_index_equal` when ``check_order=False`` and :class:`Index` has extension or object dtype (:issue:`47207`) - Fixed regression in :func:`read_excel` returning ints as floats on certain input sheets (:issue:`46988`) - Fixed regression in :meth:`DataFrame.shift` when ``axis`` is ``columns`` and ``fill_value`` is absent, ``freq`` is ignored (:issue:`47039`) -- Fixed regression in :meth:`DataFrame.to_json` when ``index`` is of the type ``PeriodIndex`` causing a segmentation violation (:issue:`46683`) +- Fixed regression in :meth:`DataFrame.to_json` when ``index`` is of the type :class:`PeriodIndex` causing a segmentation violation (:issue:`46683`) .. --------------------------------------------------------------------------- From 581bd4a40c8dc0e7cb242831d0e1506cdd65556b Mon Sep 17 00:00:00 2001 From: Robert de Vries Date: Mon, 20 Jun 2022 22:07:39 +0200 Subject: [PATCH 5/6] Reword sentence to clarify --- doc/source/whatsnew/v1.4.3.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v1.4.3.rst b/doc/source/whatsnew/v1.4.3.rst index 58a6121a370b3..867ec64f458ed 100644 --- a/doc/source/whatsnew/v1.4.3.rst +++ b/doc/source/whatsnew/v1.4.3.rst @@ -30,7 +30,7 @@ Fixed regressions - Fixed regression in :func:`assert_index_equal` when ``check_order=False`` and :class:`Index` has extension or object dtype (:issue:`47207`) - Fixed regression in :func:`read_excel` returning ints as floats on certain input sheets (:issue:`46988`) - Fixed regression in :meth:`DataFrame.shift` when ``axis`` is ``columns`` and ``fill_value`` is absent, ``freq`` is ignored (:issue:`47039`) -- Fixed regression in :meth:`DataFrame.to_json` when ``index`` is of the type :class:`PeriodIndex` causing a segmentation violation (:issue:`46683`) +- Fixed regression in :meth:`DataFrame.to_json` causing a segmentation violation when :class:`DataFrame` is created with an ``index`` of the type :class:`PeriodIndex` (:issue:`46683`) .. --------------------------------------------------------------------------- From 521dfeb80fbba8f96518004a2c9f11fc79e5f82a Mon Sep 17 00:00:00 2001 From: Robert de Vries Date: Mon, 20 Jun 2022 22:37:18 +0200 Subject: [PATCH 6/6] Add parameter to index --- doc/source/whatsnew/v1.4.3.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v1.4.3.rst b/doc/source/whatsnew/v1.4.3.rst index 867ec64f458ed..4034655ccd325 100644 --- a/doc/source/whatsnew/v1.4.3.rst +++ b/doc/source/whatsnew/v1.4.3.rst @@ -30,7 +30,7 @@ Fixed regressions - Fixed regression in :func:`assert_index_equal` when ``check_order=False`` and :class:`Index` has extension or object dtype (:issue:`47207`) - Fixed regression in :func:`read_excel` returning ints as floats on certain input sheets (:issue:`46988`) - Fixed regression in :meth:`DataFrame.shift` when ``axis`` is ``columns`` and ``fill_value`` is absent, ``freq`` is ignored (:issue:`47039`) -- Fixed regression in :meth:`DataFrame.to_json` causing a segmentation violation when :class:`DataFrame` is created with an ``index`` of the type :class:`PeriodIndex` (:issue:`46683`) +- Fixed regression in :meth:`DataFrame.to_json` causing a segmentation violation when :class:`DataFrame` is created with an ``index`` parameter of the type :class:`PeriodIndex` (:issue:`46683`) .. ---------------------------------------------------------------------------