Skip to content
Merged
Changes from 1 commit
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
81 changes: 39 additions & 42 deletions pandas/_libs/src/vendored/ujson/python/objToJSON.c
Original file line number Diff line number Diff line change
Expand Up @@ -181,59 +181,56 @@ static TypeContext *createTypeContext(void) {
}

static PyObject *get_values(PyObject *obj) {
PyObject *typ = NULL;
PyObject *arr = NULL;
PyObject *values = NULL;

if (object_is_index_type(obj) || object_is_series_type(obj)) {
// The special cases to worry about are dt64tz and category[dt64tz].
// In both cases we want the UTC-localized datetime64 ndarray,
// without going through and object array of Timestamps.
if (PyObject_HasAttrString(obj, "tz")) {
PyObject *tz = PyObject_GetAttrString(obj, "tz");
if (tz != Py_None) {
// Go through object array if we have dt64tz, since tz info will
// be lost if values is used directly.
Py_DECREF(tz);
values = PyObject_CallMethod(obj, "__array__", NULL);
return values;
}
Py_DECREF(tz);
}
if (!(object_is_index_type(obj) || object_is_series_type(obj))) {
PyObject *typeRepr = PyObject_Repr((PyObject *)Py_TYPE(obj));
PyErr_Format(PyExc_ValueError, "get_values expected Series/Index, got %R",
typeRepr);
Py_DECREF(typeRepr);
return NULL;
}

// MultiIndex raises on .array -> go through .values to get numpy array
typ = PyObject_GetAttrString(obj, "_typ");
Comment thread
jbrockmendel marked this conversation as resolved.
if (PyUnicode_Check(typ) &&
PyUnicode_CompareWithASCIIString(typ, "multiindex") == 0) {
Py_DECREF(typ);
values = PyObject_GetAttrString(obj, "values");
Comment thread
jbrockmendel marked this conversation as resolved.
if (values == NULL) {
// Clear so we can subsequently try another method
PyErr_Clear();
} else if (PyObject_HasAttrString(values, "_values_for_json")) {
// We have gotten an ExtensionArray
PyObject *array_values =
PyObject_CallMethod(values, "_values_for_json", NULL);
Py_DECREF(values);
values = array_values;
} else if (!PyArray_CheckExact(values)) {
// Didn't get a numpy array, so keep trying
Py_DECREF(values);
values = NULL;
PyErr_SetString(PyExc_ValueError,
"Error retrieving .values from MultiIndex object");
return NULL;
}
return values;
}
Py_DECREF(typ);

if (values == NULL) {
PyObject *typeRepr = PyObject_Repr((PyObject *)Py_TYPE(obj));
PyObject *repr;
if (PyObject_HasAttrString(obj, "dtype")) {
PyObject *dtype = PyObject_GetAttrString(obj, "dtype");
repr = PyObject_Repr(dtype);
Py_DECREF(dtype);
} else {
repr = PyUnicode_FromString("<unknown dtype>");
}
// For all other cases, call _values_for_json on the underlying array
arr = PyObject_GetAttrString(obj, "array");
if (arr == NULL) {
PyErr_SetString(PyExc_ValueError,
"Error retrieving .array from Index/Series object");
return NULL;
}

PyErr_Format(PyExc_ValueError, "%R or %R are not JSON serializable yet",
repr, typeRepr);
Py_DECREF(repr);
Py_DECREF(typeRepr);
values = PyObject_CallMethod(arr, "_values_for_json", NULL);

if (values == NULL) {
Py_DECREF(arr);
PyErr_SetString(PyExc_ValueError, "Error calling _values_for_json");
return NULL;
}

if (!PyArray_CheckExact(values)) {
PyErr_Format(PyExc_ValueError,
"_values_for_json should return a numpy array");
Py_DECREF(values);
Py_DECREF(arr);
return NULL;
}
Py_DECREF(arr);
return values;
}

Expand Down
Loading