Skip to content

BUG: bug in json serialization when frame has mixed types #10306

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 1 commit into from
Jun 7, 2015
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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.16.2.txt
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ Bug Fixes
- Bug where some of the nan funcs do not have consistent return dtypes (:issue:`10251`)
- Bug in groupby.apply aggregation for Categorical not preserving categories (:issue:`10138`)
- Bug in ``to_csv`` where ``date_format`` is ignored if the ``datetime`` is fractional (:issue:`10209`)
- Bug in ``DataFrame.to_json`` with mixed data types (:issue:`10289`)

- Bug in cache updating when consolidating (:issue:`10264`)

Expand Down
27 changes: 27 additions & 0 deletions pandas/io/tests/test_json/test_pandas.py
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,33 @@ def test_frame_empty_mixedtype(self):
self.assertTrue(df._is_mixed_type)
assert_frame_equal(read_json(df.to_json(), dtype=dict(df.dtypes)), df)

def test_frame_mixedtype_orient(self): # GH10289
vals = [[10, 1, 'foo', .1, .01],
[20, 2, 'bar', .2, .02],
[30, 3, 'baz', .3, .03],
[40, 4, 'qux', .4, .04]]

df = DataFrame(vals, index=list('abcd'),
columns=['1st', '2nd', '3rd', '4th', '5th'])

self.assertTrue(df._is_mixed_type)
right = df.copy()

for orient in ['split', 'index', 'columns']:
inp = df.to_json(orient=orient)
left = read_json(inp, orient=orient, convert_axes=False)
assert_frame_equal(left, right)

right.index = np.arange(len(df))
inp = df.to_json(orient='records')
left = read_json(inp, orient='records', convert_axes=False)
assert_frame_equal(left, right)

right.columns = np.arange(df.shape[1])
inp = df.to_json(orient='values')
left = read_json(inp, orient='values', convert_axes=False)
assert_frame_equal(left, right)

def test_v12_compat(self):
df = DataFrame(
[[1.56808523, 0.65727391, 1.81021139, -0.17251653],
Expand Down
1 change: 1 addition & 0 deletions pandas/src/ujson/lib/ultrajson.h
Original file line number Diff line number Diff line change
Expand Up @@ -309,5 +309,6 @@ typedef struct __JSONObjectDecoder
} JSONObjectDecoder;

EXPORTFUNCTION JSOBJ JSON_DecodeObject(JSONObjectDecoder *dec, const char *buffer, size_t cbBuffer);
EXPORTFUNCTION void encode(JSOBJ, JSONObjectEncoder *, const char *, size_t);

#endif
4 changes: 2 additions & 2 deletions pandas/src/ujson/lib/ultrajsondec.c
Original file line number Diff line number Diff line change
Expand Up @@ -803,7 +803,7 @@ FASTCALL_ATTR JSOBJ FASTCALL_MSVC decode_object( struct DecoderState *ds)
return NULL;
}

if (!ds->dec->objectAddKey (ds->prv, newObj, itemName, itemValue))
if (!ds->dec->objectAddKey (ds->prv, newObj, itemName, itemValue))
{
ds->dec->releaseObject(ds->prv, newObj, ds->dec);
ds->dec->releaseObject(ds->prv, itemName, ds->dec);
Expand Down Expand Up @@ -907,7 +907,7 @@ JSOBJ JSON_DecodeObject(JSONObjectDecoder *dec, const char *buffer, size_t cbBuf
setlocale(LC_NUMERIC, locale);
free(locale);
}
else
else
{
ret = decode_any (&ds);
}
Expand Down
9 changes: 7 additions & 2 deletions pandas/src/ujson/python/objToJSON.c
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,8 @@ enum PANDAS_FORMAT
//#define PRINTMARK() fprintf(stderr, "%s: MARK(%d)\n", __FILE__, __LINE__)
#define PRINTMARK()

int PdBlock_iterNext(JSOBJ, JSONTypeContext *);

// import_array() compat
#if (PY_VERSION_HEX >= 0x03000000)
void *initObjToJSON(void)
Expand Down Expand Up @@ -835,7 +837,10 @@ char *PdBlock_iterGetName(JSOBJ obj, JSONTypeContext *tc, size_t *outLen)
}
else
{
idx = npyarr->index[npyarr->stridedim - npyarr->inc] - 1;
idx = GET_TC(tc)->iterNext != PdBlock_iterNext
? npyarr->index[npyarr->stridedim - npyarr->inc] - 1
: npyarr->index[npyarr->stridedim];

NpyArr_getLabel(obj, tc, outLen, idx, npyarr->rowLabels);
}
return NULL;
Expand Down Expand Up @@ -2374,7 +2379,7 @@ void Object_beginTypeContext (JSOBJ _obj, JSONTypeContext *tc)
}
goto INVALID;
}
encode (tmpObj, enc, NULL, 0);
encode (tmpObj, (JSONObjectEncoder*) enc, NULL, 0);
Py_DECREF(tmpObj);
goto INVALID;
}
Expand Down