Skip to content

Commit

Permalink
Backport PR #47431 on branch 1.4.x (Fix segmentation fault when JSON …
Browse files Browse the repository at this point in the history
…serializing a PeriodIndex) (#47457)

Backport PR #47431: Fix segmentation fault when JSON serializing a PeriodIndex

Co-authored-by: Robert de Vries <rhdv@xs4all.nl>
  • Loading branch information
meeseeksmachine and roberthdevries committed Jun 22, 2022
1 parent 2d370af commit a70b494
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 1 deletion.
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.4.3.rst
Expand Up @@ -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` causing a segmentation violation when :class:`DataFrame` is created with an ``index`` parameter of the type :class:`PeriodIndex` (:issue:`46683`)

.. ---------------------------------------------------------------------------
Expand Down
4 changes: 3 additions & 1 deletion pandas/_libs/src/ujson/python/objToJSON.c
Expand Up @@ -228,8 +228,10 @@ 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);
Expand Down
7 changes: 7 additions & 0 deletions pandas/tests/io/json/test_ujson.py
Expand Up @@ -24,6 +24,7 @@
DatetimeIndex,
Index,
NaT,
PeriodIndex,
Series,
Timedelta,
Timestamp,
Expand Down Expand Up @@ -1242,3 +1243,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() == "{}"

0 comments on commit a70b494

Please sign in to comment.